this is my first question in stackoverflow, usually I’m used to searching the internet for an answer, but this time i could not find any answer for this question.
Simply my problem is a query takes TOO long to execute while it’s a simple join between 2 tables
I’ll post my query first then i will post more details about my system:
SELECT * FROM tbl_item
LEFT JOIN (SELECT * FROM tbl_item_details) AS tbl_item_details
ON tbl_item.item_id = tbl_item_details.item_details_item_id
WHERE item_active = 1 ORDER BY item_views DESC LIMIT 0,5
Here is my tables structure:
CREATE TABLE `tbl_item` (
`item_id` int(11) NOT NULL AUTO_INCREMENT,
`item_views` int(11) NOT NULL,
`item_active` tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`item_id`)
) ENGINE=InnoDB AUTO_INCREMENT=821 DEFAULT CHARSET=utf8
tbl_item_details:
CREATE TABLE `tbl_item_details` (
`item_details_id` int(11) NOT NULL AUTO_INCREMENT,
`item_details_title` varchar(255) NOT NULL,
`item_details_content` longtext NOT NULL,
`item_details_item_id` int(11) NOT NULL,
PRIMARY KEY (`item_details_id`),
KEY `itm_dt_itm_id` (`item_details_item_id`),
CONSTRAINT `tbl_item_details_ibfk_1` FOREIGN KEY (`itm_dt_itm_id`) REFERENCES `tbl_item` (`itm_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=364 DEFAULT CHARSET=utf8
here is EXPLAIN query output:
id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY tbl_item ALL NULL NULL NULL NULL 358 Using where; Using temporary; Using filesort 1 PRIMARY ALL NULL NULL NULL NULL 358 2 DERIVED tbl_item_details ALL NULL NULL NULL NULL 422
each tables has ONLY 350 rows, and the big table (tbl_item_details) 1.5 MB so you see the tables are pretty small.
BASICALLY, the above query takes almost 4 seconds to execute on the following system:
CPU: Intel(R) Pentium(R) 4 CPU 3.20GHz (2 CPUs), ~3.2GHz
RAM: 3 GB
Mysql: 5.1.33 (included with wamp)
BEFORE, anyone suggests a solution here is what I tried, what worked and what did not:
things that worked and the query took much less time (0.06 seconds):
- I tried removing ORDER BY I tried removing item_details_content from
- the select I tried using INNER JOIN instead and it worked I tried
- switching the tables in the join so the INNER table became OUTER and vice versa
I CANNOT use INNER JOIN because there might be rows in tbl_item that does not have matches in tbl_item_details and i want these records
things i tried which DID NOT work:
- I tried adding an index to item_views (DID NOT WORK)
- I tried removing the foreign keys constraints
- I tried witching the tables engine to MyIsam
Obviously the problem happens when mysql is sorting the date and faces (relatively) large data in item_details_content, so if we get rid of one of the things (either sorting or the column item_details_content) it works just fine.
but the thing is, this should not happen! cos the table has really very small data considering it’s just 350 rows with total of 1.5 MB! mysql should be able to handle much more data than that.
PLEASE, before suggesting drastic changes to the query structure, I’m afraid that is not possible, because I’ve been working on this framework for a while and the query is dynamically generated, and changes to the query might mean days of work BUT your suggestions are always welcome.
P.S: I tried this query on a powerful server (core i7 and 8 GB RAM) and it took 0.3 seconds which is still too long for such a database
Thanks a million
Have you tried:
I really don’t think you need the nested subquery that just selects * from tbl_item_details — just use the table.