Note that there are some explanatory texts on larger screens.

plurals
  1. POA Better way to optimize these MySQL queries
    text
    copied!<p>I have two MySQL tables:</p> <ul> <li>attributes (attributeid, name)</li> <li>productsattributes (productid, attributeid, displayvalue)</li> </ul> <p>The required is for each attribute name called "Product Type" get all other attributes associated with this "Product Type". As an example — attributes table will look like:</p> <pre><code> attributeid name 1 A 2 B 3 Product Type 4 D </code></pre> <p>productsattributes table will look like:</p> <pre><code>productid attributeid displayvalue 1 3 FAN 1 1 Brown 1 2 Stand 2 3 FAN 2 4 D 3 3 CAR 3 4 imported </code></pre> <p>So the final result should be:</p> <pre><code>FAN (A,B, Product Type,D) CAR (Product Type, imported) </code></pre> <p>Here is my try:</p> <ul> <li>first I get all the "displayvalues" from productattributes:</li> </ul> <pre class="lang-sql prettyprint-override"><code>SELECT DISTINCT displayvalue FROM productsttributes WHERE attributeid = 3; </code></pre> <ul> <li>then I loop through each "displayvalues" to find the other attributes:</li> </ul> <pre class="lang-sql prettyprint-override"><code>SELECT a.name FROM attributes a INNER JOIN productsattributes pa ON pa.attributeid = a.attributeid AND productid in ( SELECT productid FROM productsttributes WHERE dispalyvale = '$displayvalue') ORDER BY a.name; </code></pre> <p>The problem is the productattributes table has about 7 million rows, so my script is taking forever .. of course I am not looking for 10 minutes solution but at least it will improve my queries a bit.</p>
 

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