Mysql自内连接查询?

时间:2012-10-23 12:00:15

标签: mysql performance database-performance

当我在mysql中运行以下查询时,显示记录需要78秒。是否有其他方式来编写此查询。这是我的 mysql查询 - > “

select distinct nuqta1.post_id from wp_postmeta as nuqta1 
inner join wp_postmeta as nuqta2 on (nuqta1.post_id = nuqta2.post_id) 
inner join wp_postmeta as nuqta4 on (nuqta1.post_id = nuqta4.post_id) 
inner join wp_postmeta as nuqta5 on (nuqta1.post_id = nuqta5.post_id) 
inner join wp_postmeta as nuqta6 on (nuqta1.post_id = nuqta6.post_id) 
inner join wp_postmeta as nuqta7 on (nuqta1.post_id = nuqta7.post_id) 
inner join wp_postmeta as nuqta8 on (nuqta1.post_id = nuqta8.post_id) 
inner join wp_postmeta as nuqta9 on (nuqta1.post_id = nuqta9.post_id) 
inner join wp_postmeta as nuqta10 on (nuqta1.post_id = nuqta10.post_id) 
inner join wp_postmeta as nuqta11 on (nuqta1.post_id = nuqta11.post_id) 
inner join wp_postmeta as nuqta12 on (nuqta1.post_id = nuqta12.post_id) 
where (nuqta2.meta_key = 'checkin' and nuqta2.meta_value LIKE '%10/31/2012%') 
and (nuqta4.meta_key = 'guests' and nuqta4.meta_value ='1') 
and (nuqta5.meta_key = 'roomtype' and nuqta5.meta_value LIKE '%Entire home/apt%') 
and (nuqta6.meta_key = 'price' and cast(nuqta6.meta_value as signed) BETWEEN '10' and '99999') 
and (nuqta7.meta_key = 'amenities' and nuqta7.meta_value LIKE '%Wireless Internet%') 
and (nuqta8.meta_key = 'amenities' and nuqta8.meta_value LIKE '%TV%') 
and (nuqta9.meta_key = 'amenities' and nuqta9.meta_value LIKE '%Kitchen%') 
and (nuqta10.meta_key = 'amenities' and nuqta10.meta_value LIKE '%Wireless Internet%') 
and (nuqta11.meta_key = 'amenities' and nuqta11.meta_value LIKE '%TV%') 
and (nuqta12.meta_key = 'amenities' and nuqta12.meta_value LIKE '%Kitchen%') 
and 1=1 order by nuqta1.post_id asc

”。 并且我正在使用wordpress表wp_postmeta来运行此查询

2 个答案:

答案 0 :(得分:2)

此查询中有很多LIKE '%whatever%'个子句。这些子句中的每一个都必然导致wp_postmeta的全表扫描。实际上,你可以在不到十秒的时间内完成它们。

如果您对meta_value列的值有更多了解,那么您可以使用LIKE 'whatever%'(摆脱前导%通配符),这样可以加快速度。

另外还不清楚为什么你有nuqta10,nuqta11,nuqta12。那些似乎寻找与7,8,9相同的东西。考虑到搜索的成本,您可以考虑将其删除。

答案 1 :(得分:0)

尝试使用Fulltext Search

你要做的就是

SELECT
    *
FROM
    tableName
WHERE
    MATCH ( columnName ) AGAINST ( 'Keyword1', 'Keyword2', 'Keyword3' )