## 文档结构
{
"_id" : ObjectId("5d1c5c8e5abba855bee7c734"),
"is_import" : NumberInt(2),
"flg_deleted" : NumberInt(0),
"job_number" : "2019094339",
"name" : "周老师1",
"sex" : "女",
"pwd" : "123456",
"course_name" : [ "语文", "政治", "音乐", "生物"],
"grade_class" : {"高一" : ["二班", "三班" ]},
"class_str" : "高一二班;高一三班;高二二班;高二三班;高二四班;高二九班",
"avatar_url" : "avatar/20190628172631741.jpg",
"avatar_s_url" : "avatar/20190628172631741.jpg"
}
## 运行代码
import time
from pymongo import MongoClient
db = MongoClient("192.168.0.192:27017").ctfs_admin
def change_type():
## t_test 总数:29495
start_time1 = time.time()
## 对应的查询结果数量:3849
result_list1 = db.t_test.find({'course_name': {'$regex':'物'}})
time.sleep(1)
start_time2 = time.time()
debug(start_time2 - start_time1)
start_time22 = time.time()
## 对应的查询结果数量:19937
result_list2 = db.t_test.find({'class_str': {'$regex':'高一二班'}})
time.sleep(1)
start_time3 = time.time()
debug(start_time3 - start_time22)
if __name__ == '__main__':
change_type()
## 控制台输出
Wed Jul 03 16:13:18 - 1.000544786453247
Wed Jul 03 16:13:19 - 3849
Wed Jul 03 16:13:20 - 1.0005791187286377
Wed Jul 03 16:13:21 - 19937
总结:字符串相对列表来说,查询性能更优,数据存储结构偏向于字符串。
补充:excel导入5000条数据,for循环处理耗时约 0.055 s,批量插入 耗时约 0.515 s,而for循环逐条插入耗时约50s。
========================加索引分界线==================
网友评论