【发布时间】:2013-09-11 17:04:16
【问题描述】:
我需要一些帮助来重组我的数据库,以便以下选择查询更加高效。
问题是表post 中的每个条目都有一个类别列,其中列表以逗号分隔。
查询
SELECT id, short_story, SUBSTRING( full_story, 1, 15 ) AS full_story, xfields, title, category, alt_name, comm_num, allow_comm, allow_rate,
FIXED , rating, vote_num, news_read, votes, editdate, editor, reason, view_edit, tags
FROM post
LEFT JOIN post_plus ON ( post.id = post_plus.news_id )
WHERE category REGEXP '[[:<:]](130|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|21|22|24|25|26|27|29|133|135|125|20|132)[[:>:]]'
AND category REGEXP '[[:<:]](73)[[:>:]]'
AND approve =1
ORDER BY FIXED DESC , DATE DESC
LIMIT 98 , 7
这个列表很长,因为我有几个主要类别和很多子类别。目前它正在使用 ragexp 扫描整个表并搜索正确的匹配项。当我检查process list 时,我看到大量上述查询,状态为Creating sort index,并且我的cpu 100% 使用
说明我使用了正确的索引:
+----+-------------+---------------+------+---------------+---------+---------+--------------------+------+-----------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------------+------+---------------+---------+---------+--------------------+------+-----------------------------+
| 1 | SIMPLE | post | ref | approve | approve | 1 | const | 9593 | Using where; Using filesort |
| 1 | SIMPLE | post_plus | ref | news_id | news_id | 5 | online.post.id | 1 | NULL |
+----+-------------+---------------+------+---------------+---------+---------+--------------------+------+-----------------------------+
构建数据库以处理此任务的最佳方法是什么?
【问题讨论】:
-
列上的任何正则表达式匹配都会使表上的任何索引变得无用,并且每个查询都需要全表扫描。
标签: mysql performance select join