473,388 Members | 1,346 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Inner Join Queries Working Too Slow How to Use Indexes in them

Hello out there here i am having difficulty in fetching the Brands Per ctegory in the sidebar of my website

Here is the description ..
I am using the feeds and feed uploader to upload feeds and create brands and Product categories in wordpress
The Problem is there is No link Within the Product_category and product_brand and i want too show specific Brands in the
Side bar of a specific category noot the whole brands list which iis going to long down

so i tried out these heavy queries to fetch the brands as per category

but using the INNER JOIN made the databse too slow and the website keeploading without showing brands the query is working fiine the only thing i want to know is to speed up the queries for the Customers so they dont fade up

here are the queries i am using


Expand|Select|Wrap|Line Numbers
  1.                $term = get_term_by('slug',get_query_var('term'),'product_category');
  2.  
  3.  
  4.         $q5 = "SELECT DISTINCT p1.ID
  5.                 FROM
  6.                     $wpdb->posts p1
  7.                     INNER JOIN $wpdb->term_relationships tr1 ON (p1.ID = tr1.object_id)
  8.                     INNER JOIN $wpdb->term_taxonomy tt1 ON (tr1.term_taxonomy_id = tt1.term_taxonomy_id)
  9.                 WHERE tt1.term_id = '$term->term_id'
  10.                 ";
  11.             $q2 = "
  12.                 SELECT tt2.term_id
  13.                 FROM
  14.                     $wpdb->posts p2
  15.                     INNER JOIN $wpdb->term_relationships tr2 ON (p2.ID = tr2.object_id)
  16.                     INNER JOIN $wpdb->term_taxonomy tt2 ON (tr2.term_taxonomy_id = tt2.term_taxonomy_id)
  17.                 WHERE p2.ID IN ($q5)
  18.                 ";
  19.             $q3 = "
  20.                 SELECT DISTINCT tt3.*
  21.                 FROM
  22.                     $wpdb->posts p3
  23.                     INNER JOIN $wpdb->term_relationships tr3 ON (p3.ID = tr3.object_id)
  24.                     INNER JOIN $wpdb->term_taxonomy tt3 ON (tr3.term_taxonomy_id = tt3.term_taxonomy_id)
  25.                     INNER JOIN $wpdb->terms t3 ON t3.term_id = tt3.term_id
  26.                 WHERE 1=1
  27.                     AND tt3.term_id IN ($q2)
  28.                     AND tt3.taxonomy = 'product_brand'
  29.                 ORDER BY t3.name ASC
  30.                 ";
  31.         $brands = $wpdb->get_results($q3); 


The first two queries runs fine but the last one make the datbase query too slow
so any help please
Jan 8 '13 #1

✓ answered by Rabbit

It is unlikely that it is the join that's the cause of the slowness. A more likely culprit is the IN clause you're using. IN clauses can be extremely slow and are often better expressed as joins. You should rewrite your query using a join instead of an IN and that could fix your issue.

1 4015
Rabbit
12,516 Expert Mod 8TB
It is unlikely that it is the join that's the cause of the slowness. A more likely culprit is the IN clause you're using. IN clauses can be extremely slow and are often better expressed as joins. You should rewrite your query using a join instead of an IN and that could fix your issue.
Jan 8 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Ike | last post by:
Oh I have a nasty query which runs incredibly slowly. I am running MySQL 4.0.20-standard. Thus, in trying to expedite the query, I am trying to set indexes in my tables. My query requires four...
6
by: Thomas Beutin | last post by:
Hi, i've a speed problem withe the following statement: SELECT DISTINCT pz.l1_id, pz.l2_id, pz.l3_id, pz.l4_id FROM ot_adresse AS a, ot_produkt AS p LEFT OUTER JOIN ot_kat_prod AS pz ON (...
23
by: Brian | last post by:
Hello All - I am wondering if anyone has any thoughts on which is better from a performance perspective: a nested Select statement or an Inner Join. For example, I could do either of the...
2
by: Darryl Kerkeslager | last post by:
The following SQL does as I intend, but ... SELECT offender_id, off_name, inv_ppo_id, add_note, add_zip_id, it_name AS Purpose FROM (offender INNER JOIN (investigation INNER JOIN...
3
by: moldster | last post by:
Hi, I'm at my wits end! I have two large tables one with 1.2mill one with 2.3 mill and they are very wide tables. I have a select with an inner join. All columns used in the join are contained...
6
by: dmonroe | last post by:
hi group -- Im having a nested inner join problem with an Access SQl statement/Query design. Im running the query from ASP and not usng the access interface at all. Here's the tables: ...
52
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible...
18
by: Dave | last post by:
Guys I am really stuck on this one. Any help or suggestions would be appreciated. We have a large table which seemed to just hit some kind of threshold. They query is somewhat responsive when...
14
by: cjakeman | last post by:
Hi, Solved a little mystery yesterday when I built a form that combined 2 tables with a 1:M relationship and relational integrity. All the correct data was visible on the form but, if I tried to...
0
by: katupilar | last post by:
I am writing a tool to interface with a couple of tables in SQL Server 2000. I usually write my queries with an Inner Join to bring in fields from seperate tables and load that to a DataSet. I'm...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.