Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 使用JSON_搜索的慢速计数(*)_Mysql_Innodb - Fatal编程技术网

Mysql 使用JSON_搜索的慢速计数(*)

Mysql 使用JSON_搜索的慢速计数(*),mysql,innodb,Mysql,Innodb,我有一个表,它以JSON格式为每个页面存储标记。当前表有大约1000000条记录。 当我跑的时候 SELECT COUNT(*) FROM alltags WHERE tagtype = 'pages' AND JSON_SEARCH(tags, 'one', 'Hampden') IS NOT NULL; 查询大约需要20秒 +----------+ | COUNT(*) | +----------+ | 23500 | +----------+ 1 row in set (19.20

我有一个表,它以JSON格式为每个页面存储标记。当前表有大约1000000条记录。 当我跑的时候

SELECT COUNT(*) FROM alltags WHERE tagtype = 'pages' AND JSON_SEARCH(tags, 'one', 'Hampden') IS NOT NULL;
查询大约需要20秒

+----------+
| COUNT(*) |
+----------+
|    23500 |
+----------+
1 row in set (19.20 sec)
无论我使用InnoDB还是MyISAM引擎,查询都很慢。 我得到了tagtype、tags和带有tags列的tagtype的索引。我需要分页结果的确切数目。有什么方法可以加快SELECT COUNT查询的速度吗?选择实际数据是非常即时的

SELECT id FROM alltags WHERE tagtype = 'pages' AND JSON_SEARCH(tags, 'one', 'Hampden') IS NOT NULL LIMIT 0,10;

+------+
| id   |
+------+
| 5072 |
| 5075 |
| 5078 |
| 5081 |
| 5084 |
| 5087 |
| 5090 |
| 5093 |
| 5096 |
| 5099 |
+------+
10 rows in set (0.01 sec)
这些是索引

SHOW INDEX FROM alltags;

+---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table   | Non_unique | Key_name  | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| alltags |          0 | PRIMARY   |            1 | id          | A         |      896649 |     NULL | NULL   |      | BTREE      |         |               |
| alltags |          1 | tagtype   |            1 | tagtype     | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| alltags |          1 | keyword   |            1 | keyword     | A         |          33 |     NULL | NULL   |      | BTREE      |         |               |
| alltags |          1 | tags      |            1 | tags        | A         |       89665 |     NULL | NULL   | YES  | BTREE      |         |               |
+---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
解释结果:

选择数据

 EXPLAIN SELECT id FROM alltags WHERE tagtype = 'pages' AND JSON_SEARCH(tags, 'one', 'Hampden') IS NOT NULL;

+----+-------------+---------+------------+------+---------------+---------+---------+-------+--------+----------+-------------+
| id | select_type | table   | partitions | type | possible_keys | key     | key_len | ref   | rows   | filtered | Extra       |
+----+-------------+---------+------------+------+---------------+---------+---------+-------+--------+----------+-------------+
|  1 | SIMPLE      | alltags | NULL       | ref  | tagtype       | tagtype | 7       | const | 892372 |   100.00 | Using where |
+----+-------------+---------+------------+------+---------------+---------+---------+-------+--------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
选择计数

 EXPLAIN SELECT COUNT(*) FROM alltags WHERE tagtype = 'pages' AND JSON_SEARCH(tags, 'one', 'Hampden') IS NOT NULL;

+----+-------------+---------+------------+------+---------------+---------+---------+-------+--------+----------+-------------+
| id | select_type | table   | partitions | type | possible_keys | key     | key_len | ref   | rows   | filtered | Extra       |
+----+-------------+---------+------------+------+---------------+---------+---------+-------+--------+----------+-------------+
|  1 | SIMPLE      | alltags | NULL       | ref  | tagtype       | tagtype | 7       | const | 892372 |   100.00 | Using where |
+----+-------------+---------+------------+------+---------------+---------+---------+-------+--------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

不是
COUNT
慢,而是
JSON\u SEARCH
表上的索引是什么?@zerkms为什么检索实际数据真的很快?@JohnBaker你怎么知道它快?您只能使用
限制0,10运行类似的查询。难怪扫描10行比扫描23.5k行快。@zerkms:)没问题