2016-03-28 4 views
0

私はどのように私が最適化できるか見ていない質問がありますが、多分誰かにそれについてのいくつかの洞察力があります。ここで質問の改善が遅くなります

は私のテーブルです:ここでは

 comments 
     --------- 
      +-------------+---------------+------+-----+---------+----------------+ 
      | Field  | Type   | Null | Key | Default | Extra   | 
      +-------------+---------------+------+-----+---------+----------------+ 
      | commentid | int(11)  | NO | PRI | NULL | auto_increment | 
      | parentid | int(11)  | YES |  | 0  |    | 
      | refno  | int(11)  | YES |  | 0  |    | 
      | createdate | int(11)  | YES | MUL | 0  |    | 
      | remoteip | varchar(80) | YES |  |   |    | 
      | fingerprint | varchar(50) | YES |  |   |    | 
      | locid  | int(11)  | YES | MUL | 0  |    | 
      | clubid  | int(11)  | YES |  | 0  |    | 
      | profileid | int(11)  | YES | MUL | 0  |    | 
      | userid  | int(11)  | YES | MUL | 0  |    | 
      | global  | int(11)  | YES |  | 0  |    | 
      | official | int(11)  | YES |  | 0  |    | 
      | legacyuser | int(11)  | YES | MUL | 0  |    | 
      | mediaid  | int(11)  | YES |  | 0  |    | 
      | status  | int(11)  | YES |  | 1  |    | 
      | comment  | varchar(4000) | YES |  |   |    | 
      | likes  | int(11)  | YES |  | 0  |    | 
      | dislikes | int(11)  | YES |  | 0  |    | 
      | import  | int(11)  | YES |  | 0  |    | 
      | author  | varchar(50) | YES |  |   |    | 
      +-------------+---------------+------+-----+---------+----------------+ 

は私の表が説明です:

 | comments | CREATE TABLE `comments` (
     `commentid` int(11) NOT NULL AUTO_INCREMENT, 
     `parentid` int(11) DEFAULT '0', 
     `refno` int(11) DEFAULT '0', 
     `createdate` int(11) DEFAULT '0', 
     `remoteip` varchar(80) DEFAULT '', 
     `fingerprint` varchar(50) DEFAULT '', 
     `locid` int(11) DEFAULT '0', 
     `clubid` int(11) DEFAULT '0', 
     `profileid` int(11) DEFAULT '0', 
     `userid` int(11) DEFAULT '0', 
     `global` int(11) DEFAULT '0', 
     `official` int(11) DEFAULT '0', 
     `legacyuser` int(11) DEFAULT '0', 
     `mediaid` int(11) DEFAULT '0', 
     `status` int(11) DEFAULT '1', 
     `comment` varchar(4000) DEFAULT '', 
     `likes` int(11) DEFAULT '0', 
     `dislikes` int(11) DEFAULT '0', 
     `import` int(11) DEFAULT '0', 
     `author` varchar(50) DEFAULT '', 
     PRIMARY KEY (`commentid`), 
     KEY `comments_locid` (`locid`), 
     KEY `comments_userid` (`userid`), 
     KEY `idx_legacyusers` (`legacyuser`), 
     KEY `profile_index` (`profileid`), 
     KEY `comments_createdate` (`createdate`), 
     KEY `compound_for_comments` (`locid`,`global`,`status`), 
     KEY `global` (`global`), 
     KEY `status` (`status`) 
    ) ENGINE=InnoDB AUTO_INCREMENT=3848451 DEFAULT CHARSET=latin1 

そして最後に、ここで私のクエリは次のとおりです。

SELECT c.createdate commentdate 
    FROM comments c 
WHERE status > 0 
    AND locid 
    IN 
    (SELECT locid 
     FROM locations 
     WHERE state = 'NJ' 
    ) 
ORDER 
    BY c.createdate DESC 
LIMIT 15 

LOCID上のインデックスがありますサブクエリの状態は

ご覧のとおり、外側のクエリに使用するlocidsを策定するために、ロケーションテーブルに対して副選択があります。

改善の余地があります。

+0

あなたのサブクエリは、おそらく「*」 'join' ... –

+0

一歩を取り除くことであろう通常のように書き換えることができます。また、JOINがもはやファッショナブルではないように見えることに感謝しますが、パフォーマンスのメリットが得られる時間がありました。 – Strawberry

+0

BOTHテーブルのCREATE TABLEステートメントを見ることができます。そして、クエリのためのEXPLAIN – Strawberry

答えて

0

これは同等であり、おそらく少し速いかもしれません。

select *,comments.createdate as commentdate 
from comments FORCE INDEX (locid_status) 
inner join locations FORCE INDEX (your_index_name) on (comments.locid = locations.locid and locations.state='NJ') 
where comments.status>0 
order by comments.createdate desc limit 15 
+0

それはあまり改善されませんでした。 〜0.5秒速くなります。 – slicks1

+0

何レコードですか? scx_comments(locid)とscl_locations(.locid)のインデックスが必要です。 – scaisEdge

+0

これらのカラムにインデックスがあります。データベースにはおよそ250万レコードがあります。 – slicks1

0

ロケーションが小さなテーブルの場合は、サブクエリを使用するよりも高速に結合できます。

select *,comments.createdate as commentdate 
from comments as c, locations as l 
where status > 0 
and l.state = 'NJ' 
and l.locid = c.locid 
order by c.createdate desc limit 15; 
関連する問題