English 中文(简体)
MySQL 满足需求
原标题:MySQL Query Needs Optimizations

页: 1 下表:

id : int (255)
name: char (100)
last_comment_target: int(100)
last_comment_date: datetime

该表有大约1.3米增长。

<代码>PKEY和BTREE>,id,last_comment_target/code>,和last_comment_date

而且,我试图进行一系列的问答:

SELECT * FROM users 
WHERE id IN (1,2,3,5,...[around 5000 ids]) 
AND last_comment_target > 0 
ORDER BY last_comment_dt DESC LIMIT 0,20;

有时,问题可能长达3秒。 我想知道,是否有更好的办法优化这一询问。 或者,如果能够重写这一询问。

非常感谢您的帮助。

最佳回答
SELECT u.* 
FROM 
    users u
    JOIN (
        SELECT 1 id 
        UNION ALL
        SELECT 2 id
        UNION ALL
        :
        :
        SELECT 5000 id
    ) ids ON ids.id = u.id
WHERE 
    last_comment_target > 0 
ORDER BY 
    last_comment_dt DESC 
LIMIT 0, 20;
问题回答

感谢大家的贡献。

@Karolis似乎指出,使用join而不是range的备选案文。

因此,基本上:

< * E/CN.6/2009/1。 和最后部分

<代码>EXPLAIN 说明a> 类型 of RANGE。 可以从另一个表格中产生5,000个孩子。

当我把上述内容改为:

SELECT *
FROM users u
INNER JOIN user_friends uf ON u.id = uf.to_id
AND u.last_comment_target > 0
AND uf.from_id = [id];

该表在<代码>EXPLAIN 说明 2 <代码> 类型:ref_eq_ref,其速度比range更快。

点击执行从3+秒减少到大约0.2x秒。

因此,从我结尾处学到的教益:TRY <>/em> ,如果你有一张可以从中得出的表格,则使用JOIN,而不是RANGE<><>>>>>>>>。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...