MySQL full-text index, joint index, like the query, json query speed Competition

Query background

There is a table tmp_test_course about 100,000 records, then there is a field called json outline, saved many relationship (save multiple coding, for example jy1577683381775)

We need to retrieve a specific type of data in this data 100 000, the target total amount of data: 2931Article

SELECT COUNT(*) FROM tmp_test_course WHERE `type`=5 AND del=2 AND is_leaf=1


We defined as the above type while a coding have any of the following contain (i.e. query OR)

jy1577683381775
jy1577683380808
jy1577683379178
jy1577683378676
jy1577683377617
jy1577683376672
jy1577683375903
jy1578385720787
jy1499916986208
jy1499917112460
jy1499917093400
jy1499917335579
jy1499917334770
jy1499917333339
jy1499917331557
jy1499917330833
jy1499917329615
jy1499917328496
jy1576922006950
jy1499916993558
jy1499916992308
jy1499917003454
jy1499917002952

The following four modes were listed outline query field, given the appropriate number of scan lines and the time the query

A, like inquiry

It takes 248 milliseconds

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 following analysis, a full table scan

Two, json query function

json official function
takes 196 ms, slightly faster bit

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 analysis results are as follows, or a full table scan

Third, the joint index query

Following the establishment of a joint index for the table (the index had wanted to build a type-del-is_leaf-outline, but the field is too long outline limit, so the only plus joint index of type-del-is_leaf_

ALTER TABLE tmp_test_course ADD KEY `type-del-is_leaf` (`type`,`del`,`is_leaf`)

Added to the index and then perform like json query, significantly accelerated.
like the 136 milliseconds execution, json queries 82.6 milliseconds, we can see a query for the type of use json json like functions faster than

EXPLAIN the following analysis, the number of rows of both the inquiry scan line 2931 are defined in the

Fourth, the full-text index query

Because the full-text index only supports CHAR, VARCHAR and TEXT, we need to change it JSON field definitions

ALTER TABLE tmp_test_course MODIFY `outline` VARCHAR(1024) NOT NULL DEFAULT '[]'

Adding full-text indexing

ALTER TABLE tmp_test_course ADD FULLTEXT INDEX outline (outline);

Now come back to retrieve full-text index

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')

Consuming 11.6 milliseconds, faster extremely obvious, visible full-text index regressed.

EXPLAIN following analysis, displays only the scan line

in conclusion

The following are the results of four cases

全文索引: 11.6ms
联合索引:82.6ms(json)、136ms(like)
json函数查询:196ms
like查询: 248ms

结论:全文索引 > 联合索引 > json函数查询 > like查询
数据量越大,全文索引速度越明显,就10万的量,查询速度大概比直接查询快了20倍左右,如果是百万或千万级别的表,提升差距会更加大,所以有条件还是老老实实用全文索引吧

Guess you like

Origin www.cnblogs.com/chenqionghe/p/12367268.html