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 6663273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:28:51+00:00 2026-05-26T02:28:51+00:00

I’m trying to optimize this query, and as simple as it sounds, I’m having

  • 0

I’m trying to optimize this query, and as simple as it sounds, I’m having a hard time figuring out how to speed it up.

I have two objects, a Widget and a Category (HABTM relationship), with the tables “widgets”, “categories”, and “categories_widgets” (join table). I’m trying to find all the widgets in a particular category. Simple right? Some categories can have 10-50k widgets though, and my queries are taking 1-2 seconds to complete (my site is fairly high traffic, so this is completely unacceptable).

Here’s what I’m doing now:

SELECT * FROM `categories` 
LEFT JOIN `categories_widgets` ON `categories_widgets`.`category_id`=`categories`.`id`
JOIN `widgets` ON `widgets`.`id`=`categories_widgets`.`widget_id`
ORDER BY `widgets`.`id` DESC
LIMIT 0,10

I do need to order the widgets by id descending (this is what kills the speed), and I can sometimes have decently high offsets (not sure if anything can be done about that).

Any help with this would be appreciated, or alternatively if you have any recommendations for table restructuring, that’s a possibility as well (but assuming a lot of categories and a lot of widgets with no easy way to shard).

Thank you!

  • 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-05-26T02:28:52+00:00Added an answer on May 26, 2026 at 2:28 am

    The table layout seems right.
    Why the LEFT JOIN? I made that a JOIN.
    Where is your WHERE clause? “find all the widgets in a particular category.”

    SELECT *
      FROM categories_widgets cw
      JOIN widgets w  ON w.id = cw.widgets_id
     WHERE cw.category_id = $my_cat_id
     ORDER BY w.id DESC
     LIMIT 0,10
    

    And you do have indexes on categories_widgets(category_id) and widgets(id), right?

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which 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.