【发布时间】:2012-09-17 17:43:44
【问题描述】:
我已经尽我所能来加快这个查询的速度,但仍然需要大约 2.5 秒。
该表是 images_tags(约 400 万行): 这是表格说明:
Field Type Null Key Default
image_ids int(7) unsigned NO PRI NULL
tags_id int(7) unsigned NO PRI NULL
这里是索引:
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type
images_tags 0 PRIMARY 1 image_ids A NULL NULL NULL BTREE
images_tags 0 PRIMARY 2 tags_id A 4408605 NULL NULL BTREE
images_tags 1 image_ids 1 image_ids A 734767 NULL NULL BTREE
这里是查询:
select image_ids
from images_tags
where tags_id in (1, 2, 21, 846, 3175, 4290, 6591, 9357, 9594, 14289, 43364, 135019, 151295, 208803, 704452)
group by image_ids
order by count(*) desc
limit 10
这里是查询解释:
select_type table type possible_keys key key_len ref rows Extra
SIMPLE vids_x_tags index join_tags_id join_vids_id_unique 8 NULL 4408605 Using where; Using index; Using temporary; Using filesort
目标是获得与这些标签最匹配的 10 张图像。 我尝试过处理这些变量,但几乎没有改善:
- max_heap_table_size
- tmp_table_size
- myisam_sort_buffer_size
- read_buffer_size
- sort_buffer_size
- read_rnd_buffer_size
- net_buffer_length
- preload_buffer_size
- key_buffer_size
有什么方法可以大大加快这个查询的速度吗?大约有 700K 图像并且它一直在增长,所以我不想将结果缓存超过一两天,并且必须为每个图像完成,所以重新缓存很多查询是不可能的。
【问题讨论】:
-
count(*)?确定吗?不应该是count(image_ids)吗? -
@Burhan:因为
image_ids没有空值,所以是一样的。 -
请添加 EXPLAIN 输出。
-
There are about 700K images and it's always growing, so I wouldn't want to cache the result for more than a day or 2 and re-caching that many results would be impossible.- 缓存中不是只有 10 个结果吗?如果信息的流通性不重要;您可以在停机时生成缓存,存储 10 个 ID 并仅在显示中检索它们。 -
@ricka - 每天 700K 次?为什么?您需要生成前 10 名;您可以通过每天运行查询 一次 来生成它一次,然后存储结果。那是你的前 10 名。除非我错过了为标签生成前 10 名图像的内容。
标签: mysql performance tags aggregate