Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7769997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:17:57+00:00 2026-06-01T16:17:57+00:00

this is my first question in stackoverflow, usually I’m used to searching the internet

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:17:58+00:00Added an answer on June 1, 2026 at 4:17 pm

    Have you tried:

    SELECT * FROM tbl_item 
    LEFT JOIN 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
    

    I really don’t think you need the nested subquery that just selects * from tbl_item_details — just use the table.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First look at this url: https://stackoverflow.com/questions/tagged/xoxoxo/ This directory does not exists but somehow stackoverflow
I'm new to ruby and this is my first stackoverflow question (although I've used
This is my first time asking my question at stackoverflow. I'm working on a
This is actually my very first question on Stackoverflow. I hope its not the
I think this could be my first question here in stackoverflow. It's about as
this is my first question, but I've readed StackOverflow for years. Well the thing
This is my first time using stackoverflow. I've been unable to find out the
This is my first question on stackoverflow (I have usually been able to figure
this is my first question on StackOverflow, but I think that we'll both come
This is my first question on StackOverflow.com and I'm not sure if this is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.