Note that there are some explanatory texts on larger screens.

plurals
  1. POhelp removing mysql file sort
    primarykey
    data
    text
    <p>Here is the log output:</p> <pre><code># Time: 100915 13:06:49 # User@Host: ss[ss] @ localhost [] # Query_time: 13.978355 Lock_time: 0.000029 Rows_sent: 10 Rows_examined: 562760 use ss; SET timestamp=1284574009; SELECT DISTINCT SQL_CALC_FOUND_ROWS B.*, U.username FROM sc_users AS U, sc_bookmarks AS B WHERE B.uId = U.uId AND B.bStatus = 0 GROUP BY B.bHash ORDER BY B.bModified DESC LIMIT 10; </code></pre> <p>and the explain output:</p> <pre><code>id select_type table type possible_keys key key_len ref rows Extra ---------------------------------------------------------------------------------------------------- 1 SIMPLE U ALL PRIMARY NULL NULL NULL 2 Using temporary; Using filesort 1 SIMPLE B ALL sc_bookmarks_usd NULL NULL NULL 187586 Using where; Using join buffer </code></pre> <p>UPDATE: Here is the create statement for sc_users thanks OMGPonies / BrianHooper :-)</p> <pre><code>CREATE TABLE `sc_users` ( `uId` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(25) NOT NULL DEFAULT '', `password` varchar(40) NOT NULL DEFAULT '', `uDatetime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `uModified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `name` varchar(50) DEFAULT NULL, `email` varchar(50) NOT NULL DEFAULT '', `homepage` varchar(255) DEFAULT NULL, `uContent` text, PRIMARY KEY (`uId`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ; </code></pre> <p>More details (thanks Mark Byers):</p> <p>This query is generated by the open source project semanticscuttle to generate the home-page. I do not know why the query was written as it was, as I am just getting my feet wet in this project. <a href="http://semanticscuttle.svn.sourceforge.net/viewvc/semanticscuttle/trunk/src/SemanticScuttle/Service/Bookmark.php?revision=693&amp;view=markup" rel="nofollow noreferrer">You can see the source where this sql is generated here though.</a></p> <p>I have tried adding various indexes and combined indexes with no luck speeding up the query or removing the file scan. Any tips are appreciated!</p> <p>UPDATE: The slow-down is caused by the ORDER BY statement... The query bellow runs in 4 seconds with the ORDER BY, 0.01 seconds without it. Any ideas why / how to fix it?</p> <pre><code>SELECT DISTINCT SQL_CALC_FOUND_ROWS B.*, U.username FROM sc_users AS U, sc_bookmarks AS B WHERE B.uId = U.uId AND B.bStatus = 0 GROUP BY B.bHash ORDER BY B.bModified DESC </code></pre> <p>UPDATE: NEW SQL and index Quassnoi in an answer bellow... This looked great to me, but actually increased execution time, and didn't get rid of the file sorts:</p> <pre><code>EXPLAIN SELECT SQL_CACHE b.*, u.username FROM ( SELECT DISTINCT bHash FROM sc_bookmarks b WHERE bStatus = 0 ) bd JOIN sc_bookmarks b ON b.bId = ( SELECT bId FROM sc_bookmarks bi WHERE bi.bStatus = 0 AND bi.bHash = bd.bHash ORDER BY bStatus DESC, bHash DESC, bModified DESC, bId DESC LIMIT 1 ) JOIN sc_users u ON u.uId = b.uId ORDER BY bModified ASC LIMIT 10; +----+--------------------+------------+--------+--------------------------+------------------+---------+----------+--------+---------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+--------------------+------------+--------+--------------------------+------------------+---------+----------+--------+---------------------------------+ | 1 | PRIMARY | &lt;derived2&gt; | ALL | NULL | NULL | NULL | NULL | 187565 | Using temporary; Using filesort | | 1 | PRIMARY | b | eq_ref | PRIMARY,sc_bookmarks_usd | PRIMARY | 4 | func | 1 | Using where | | 1 | PRIMARY | u | eq_ref | PRIMARY,su_idx | PRIMARY | 4 | ss.b.uId | 1 | | | 3 | DEPENDENT SUBQUERY | bi | ref | sc_bookmarks_hui,bStatus | sc_bookmarks_hui | 98 | bd.bHash | 1 | Using where; Using filesort | | 2 | DERIVED | b | ref | bStatus | bStatus | 1 | | 94556 | Using where; Using index | +----+--------------------+------------+--------+--------------------------+------------------+---------+----------+--------+---------------------------------+ </code></pre> <p>UPDATE: per the request of Quassnoi, here is the CREATE TABLE statement for sc_bookmarks.</p> <pre><code>CREATE TABLE `sc_bookmarks` ( `bId` int(11) NOT NULL AUTO_INCREMENT, `uId` int(11) NOT NULL DEFAULT '0', `bIp` varchar(40) DEFAULT NULL, `bStatus` tinyint(1) NOT NULL DEFAULT '0', `bDatetime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `bModified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `bTitle` varchar(255) NOT NULL DEFAULT '', `bAddress` varchar(1500) NOT NULL, `bDescription` text, `bPrivateNote` text, `bHash` varchar(32) NOT NULL DEFAULT '', `bVotes` int(11) NOT NULL, `bVoting` int(11) NOT NULL, `bShort` varchar(16) DEFAULT NULL, PRIMARY KEY (`bId`), KEY `sc_bookmarks_usd` (`uId`,`bStatus`,`bDatetime`), KEY `sc_bookmarks_hui` (`bHash`,`uId`,`bId`), KEY `sc_bookmarks_du` (`bDatetime`,`uId`), KEY `sc_bookmarks_modified_idx` (`bModified`), KEY `bStatus` (`bStatus`,`bHash`,`bModified`,`uId`,`bId`), KEY `bHash` (`bHash`), CONSTRAINT `sc_bookmarks_ibfk_1` FOREIGN KEY (`uId`) REFERENCES `sc_users` (`uId`), CONSTRAINT `sc_bookmarks_ibfk_2` FOREIGN KEY (`uId`) REFERENCES `sc_users` (`uId`) ) ENGINE=InnoDB AUTO_INCREMENT=187566 DEFAULT CHARSET=utf8 </code></pre> <p>UPDATE with jacobs feedback on cwiske's answer. The query is well optimized, but it does not return the same results as the original query.</p> <p>For example I have 3 entries in table "sc_bookmarks".</p> <pre><code>bId bModified bHash ------------------------------------ 2000-11-10 aaaaaa ... 2011-12-12 bbbbbb ... 2010-11-11 aaaaaa ... </code></pre> <p>The query should return only row id 1 and 2. But it return all 3 records. Seems mysql doesn't like: ´GROUP BY B.bModified DESC, B.bHash DESC´</p> <p>I tried to change ´GROUP BY B.bModified DESC, B.bHash DESC´</p> <p>to ´GROUP BY B.bModified, B.bHash GROUP BY B.bHash, B.bModified´</p> <p>but I'm still getting all 3 records.</p> <p>It works when I leave ´GROUP BY B.bHash´ but that prevents the index from working.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload