有一個表tmp_test_course大概有10萬條記錄,然后有個json字段叫outline,存了一對多關(guān)系(保存了多個編碼,例如jy1577683381775)
成都創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、元江縣網(wǎng)絡(luò)推廣、微信小程序開發(fā)、元江縣網(wǎng)絡(luò)營銷、元江縣企業(yè)策劃、元江縣品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;成都創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供元江縣建站搭建服務(wù),24小時服務(wù)熱線:13518219792,官方網(wǎng)址:www.cdcxhl.com
我們需要在這10萬條數(shù)據(jù)中檢索特定類型的數(shù)據(jù),目標(biāo)總數(shù)據(jù)量:2931
條
SELECT COUNT(*) FROM tmp_test_course WHERE `type`=5 AND del=2 AND is_leaf=1
我們在限定為上面類型的同時,還得包含下面任意一個編碼(也就是OR查詢)
jy1577683381775
jy1577683380808
jy1577683379178
jy1577683378676
jy1577683377617
jy1577683376672
jy1577683375903
jy1578385720787
jy1499916986208
jy1499917112460
jy1499917093400
jy1499917335579
jy1499917334770
jy1499917333339
jy1499917331557
jy1499917330833
jy1499917329615
jy1499917328496
jy1576922006950
jy1499916993558
jy1499916992308
jy1499917003454
jy1499917002952
下面分別列出4種方式查詢outline字段,給出相應(yīng)的查詢時間和掃描行數(shù)
耗時248毫秒
SELECT * FROM tmp_test_course
WHERE `type`=5 AND del=2 AND is_leaf=1
AND (
outline like '%jy1577683381775%'
OR outline like '%jy1577683380808%'
OR outline like '%jy1577683379178%'
OR outline like '%jy1577683378676%'
OR outline like '%jy1577683377617%'
OR outline like '%jy1577683376672%'
OR outline like '%jy1577683375903%'
OR outline like '%jy1578385720787%'
OR outline like '%jy1499916986208%'
OR outline like '%jy1499917112460%'
OR outline like '%jy1499917093400%'
OR outline like '%jy1499917335579%'
OR outline like '%jy1499917334770%'
OR outline like '%jy1499917333339%'
OR outline like '%jy1499917331557%'
OR outline like '%jy1499917330833%'
OR outline like '%jy1499917329615%'
OR outline like '%jy1499917328496%'
OR outline like '%jy1576922006950%'
OR outline like '%jy1499916993558%'
OR outline like '%jy1499916992308%'
OR outline like '%jy1499917003454%'
OR outline like '%jy1499917002952%'
)
EXPLAIN分析結(jié)果如下,全表掃描
使用函數(shù)JSON_SEARCH,更多函數(shù)請查看MySQL官方文檔
可以看到,查詢耗時196毫秒,速度稍微快了一點(diǎn)
SELECT * FROM tmp_test_course
WHERE `type`=5 AND del=2 AND is_leaf=1
AND
(
JSON_SEARCH(outline, 'one', 'jy1577683381775') IS NOT NULL OR
JSON_SEARCH(outline, 'one', 'jy1577683380808') IS NOT NULL OR
JSON_SEARCH(outline, 'one', 'jy1577683379178') IS NOT NULL OR
JSON_SEARCH(outline, 'one', 'jy1577683378676') IS NOT NULL OR
JSON_SEARCH(outline, 'one', 'jy1577683377617') IS NOT NULL OR
JSON_SEARCH(outline, 'one', 'jy1577683376672') IS NOT NULL OR
JSON_SEARCH(outline, 'one', 'jy1577683375903') IS NOT NULL OR
JSON_SEARCH(outline, 'one', 'jy1578385720787') IS NOT NULL OR
JSON_SEARCH(outline, 'one', 'jy1499916986208') IS NOT NULL OR
JSON_SEARCH(outline, 'one', 'jy1499917112460') IS NOT NULL OR
JSON_SEARCH(outline, 'one', 'jy1499917093400') IS NOT NULL OR
JSON_SEARCH(outline, 'one', 'jy1499917335579') IS NOT NULL OR
JSON_SEARCH(outline, 'one', 'jy1499917334770') IS NOT NULL OR
JSON_SEARCH(outline, 'one', 'jy1499917333339') IS NOT NULL OR
JSON_SEARCH(outline, 'one', 'jy1499917331557') IS NOT NULL OR
JSON_SEARCH(outline, 'one', 'jy1499917330833') IS NOT NULL OR
JSON_SEARCH(outline, 'one', 'jy1499917329615') IS NOT NULL OR
JSON_SEARCH(outline, 'one', 'jy1499917328496') IS NOT NULL OR
JSON_SEARCH(outline, 'one', 'jy1576922006950') IS NOT NULL OR
JSON_SEARCH(outline, 'one', 'jy1499916993558') IS NOT NULL OR
JSON_SEARCH(outline, 'one', 'jy1499916992308') IS NOT NULL OR
JSON_SEARCH(outline, 'one', 'jy1499917003454') IS NOT NULL OR
JSON_SEARCH(outline, 'one', 'jy1499917002952') IS NOT NULL
)
EXPLAIN分析結(jié)果如下,還是全表掃描
下面為該表建立一個聯(lián)合索引(本來想建一個type-del-is_leaf-outline的索引,但是outline字段太長限制,所以只加type-del-is_leaf的聯(lián)合索引
ALTER TABLE tmp_test_course ADD KEY `type-del-is_leaf` (`type`,`del`,`is_leaf`)
加入索引后再執(zhí)行l(wèi)ike和json查詢,明顯提速。
like執(zhí)行用了136毫秒,json查詢用了82.6毫秒,由此可見針對json類型使用json函數(shù)查詢比like快
EXPLAIN分析結(jié)果如下,兩者查詢掃描的行數(shù)都限定在了2931行
因?yàn)槿乃饕恢С諧HAR、VARCHAR和TEXT,我們需要把JSON字段定義改一下
ALTER TABLE tmp_test_course MODIFY `outline` VARCHAR(1024) NOT NULL DEFAULT '[]'
添加全文索引
ALTER TABLE tmp_test_course ADD FULLTEXT INDEX outline (outline);
現(xiàn)在再來用全文索引進(jìn)行檢索
SELECT * FROM tmp_test_course
WHERE `type`=5 AND del=2 AND is_leaf=1
AND
MATCH(outline) AGAINST ('jy1577683381775 jy1577683380808 jy1577683379178 jy1577683378676 jy1577683377617 jy1577683376672 jy1577683375903 jy1578385720787 jy1499916986208 jy1499917112460 jy1499917093400 jy1499917335579 jy1499917334770 jy1499917333339 jy1499917331557 jy1499917330833 jy1499917329615 jy1499917328496 jy1576922006950 jy1499916993558 jy1499916992308 jy1499917003454 jy1499917002952')
耗時11.6毫秒,速度提升極其明顯,可見全文索引的牛逼。
EXPLAIN分析結(jié)果如下,顯示只掃描了一行