English 中文(简体)
努力优化我的试金字,但是,如果按需要增加一个文件,需要很长时间。
原标题:trying to optimize mysql query but when i add ORDER BY its takes long time

这是我提出的问题。

SELECT U.id AS user_id,C.name AS country,
                CASE
                WHEN U.facebook_id > 0 THEN CONCAT(F.first_name,   ,F.last_name)
                WHEN U.twitter_id > 0 THEN T.name
                WHEN U.regular_id > 0 THEN CONCAT(R.first,   ,R.last)
                END AS name,
                FROM user U LEFT OUTER JOIN regular R
                ON U.regular_id = R.id
                LEFT OUTER JOIN twitter T
                ON U.twitter_id = T.id
                LEFT OUTER JOIN facebook F
                ON U.facebook_id = F.id
                LEFT OUTER JOIN country C
                ON U.country_id = C.id
                WHERE (CONCAT(F.first_name,   ,F.last_name) LIKE  %  OR T.name LIKE  %  OR CONCAT(R.first,   ,R.last) LIKE  % ) AND U.active = 1
                LIMIT 100

its realy fast, but in the EXPLAIN it don t show me it uses INDEXES (there is indexes). but when i add ORDER BY name before the LIMIT its takes long time why? there is a way to solve it?

tables: users 150000, regular 50000, facebook 50000, twitter 50000, country 250 and growing!

最佳回答

它需要很长时间,因为它是一个综合栏,而不是一个表列。 <代码> 姓名/代码>一栏是案件选择的结果,与多合并的简单选择不同,MySQL必须对这种数据使用不同的分类算法。

我在这里谈论的是无知,但你可以把数据储存在临时表格中,然后加以分类。 这样做可能更快,因为你可以为其制定指数,但由于储存类型不同,它所赢得的数值是一样快的。

http://www.ohchr.org。

CREATE TEMPORARY TABLE `short_select` 
       SELECT U.id AS user_id,C.name AS country,
            CASE
            WHEN U.facebook_id > 0 THEN CONCAT(F.first_name,   ,F.last_name)
            WHEN U.twitter_id > 0 THEN T.name
            WHEN U.regular_id > 0 THEN CONCAT(R.first,   ,R.last)
            END AS name,
            FROM user U LEFT OUTER JOIN regular R
            ON U.regular_id = R.id
            LEFT OUTER JOIN twitter T
            ON U.twitter_id = T.id
            LEFT OUTER JOIN facebook F
            ON U.facebook_id = F.id
            LEFT OUTER JOIN country C
            ON U.country_id = C.id
            WHERE (CONCAT(F.first_name,   ,F.last_name) LIKE  %  OR T.name LIKE  %  OR CONCAT(R.first,   ,R.last) LIKE  % ) AND U.active = 1
            LIMIT 100;

ALTER TABLE `short_select` ADD INDEX(`name`); --add successive columns if you are going to order by them as well.

SELECT * FROM `short_select`
    ORDER BY  name ; -- same as above

暂记表格在关系终止时投放,因此,您不打have,以清理这些表格,但should

问题回答

如果不实际了解你的非行结构,假设你们有所有适当的指数。 缩略语 如果只有10个牢房,如果你获得2000年增长,那么如果你将15k个牢房分成多个桌子,那么将几乎是瞬时的,那么将需要一些时间来对所回结结果进行分类。 还确保你在按你分类的领域中增加指数。 你们可能希望取得预期结果,把所有东西放在一个预想的ub中,以便更晚地进行询问(如果你问,这种结果常常会带来)

您需要分别从各个名称表中生成第1个<代码>100记录,然后将结果与<编码>用户/代码>和<代码>国家合并,并订购和限制产出:

SELECT  u.id AS user_id, c.name AS country, n.name
FROM    (
        SELECT  facebook_id AS id, CONCAT(F.first_name,    , F.last_name) AS name
        FROM    facebook
        ORDER BY
                first_name, last_name
        LIMIT 100
        UNION ALL
        SELECT  twitter_id, name
        FROM    twitter
        WHERE   twitter_id NOT IN
                (
                SELECT  facebook_id
                FROM    facebook
                )
        ORDER BY
                name
        LIMIT 100
        UNION ALL
        SELECT  regular_id, CONCAT(R.first,    , R.last)
        FROM    regular
        WHERE   regular_id NOT IN
                (
                SELECT  facebook_id
                FROM    facebook
                )
                AND 
                regular_id NOT IN
                (
                SELECT  twitter_id
                FROM    twitter
                )
        ORDER BY
                first, last
        LIMIT 100
        ) n
JOIN    user u
ON      u.id = n.id
JOIN    country с
ON      c.id = u.country_id

建立以下指数:

facebook (first_name, last_name)
twitter (name)
regular (first, last)

请注意,这一查询令与你原来的命令略有不同:在这一询问中,Ronnie James Dio将在Ronnie Scott 之后进行分类。

使用各栏的功能防止指数的使用。

CONCAT(F.first_name,   ,F.last_name)

该职能的结果没有指数化,即使各栏可能。 你们要么必须重新制定条件,单独查询名字栏,要么必须储存该功能的结果并索引(例如“全名”栏)。

如果大多数用户都很活跃,则[用户积极]指数不大可能帮助你。

我不知道你的申请是什么,但我想知道,如果你把外国钥匙推到用户桌上,而是将用户信息数据库作为其他表格中的一个外国钥匙,那么这是否比较容易。





相关问题
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 ...