1.批量查询的好处
一条一条的查询,比如说查询100条数据,要发送100次网络请求,这个开销还是很大的,如果进行批量查询100条数据的话,只需要发送一次网络请求,网络请求性能开销直接减少100倍。
2.mget的语法
(1)如果查询的document是在不同的index,不同的type,不同的id中的话
GET /_mget
{
"docs":[
{
"_index":"test_index",
"_type":"test_type",
"_id":6
},
{
"_index":"test_index",
"_type":"test_type",
"_id":8
}
]
}
运行结果:
{
"docs": [
{
"_index": "test_index",
"_type": "test_type",
"_id": "6",
"_version": 2,
"found": true,
"_source": {
"test_field1": "test1",
"test_field2": "test123"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "8",
"_version": 2,
"found": true,
"_source": {
"num": 1,
"tags": []
}
}
]
}
(2)如果查询的document是在同一个index,不同的type和id中的话
GET test_index/_mget
{
"docs":[
{
"_type":"test_type",
"_id":6
},
{
"_type":"test_type",
"_id":8
}
]
}
运行结果:同上
(3)如果查询的document是在同一个index,同一个type,不同的id中的话
GET test_index/test_type/_mget
{
"ids":[6,8]
}
运行结果:同上
3.mget的重要性
一般来说,在进行查询的时候,如果一次性要查询多条数据的话,那么一定要用batch批量操作的api,尽可能的减少网络开销次数,可能可以将性能提高数倍,甚至数十倍,非常非常之重要。