Note that there are some explanatory texts on larger screens.

plurals
  1. POOptimize MySQL query
    primarykey
    data
    text
    <p>I have a database that stores products. The two biggest tables are my products table and images table. Products: ~19.000 rows</p> <p>Images: ~34.000 rows</p> <p>Categories: ~60 rows</p> <p>Raw categories: ~1200 rows</p> <p>Brands: 700 rows</p> <p>(The other tables are only a couple of rows)</p> <p>I also have a table with brands, raw categories, static categories (the raw categories are mapped to a static category).</p> <p>I have created a Product VIEW to gather the data I need, like this:</p> <pre><code>CREATE OR REPLACE VIEW `jos_clothes_view_products` AS SELECT tbl.*, r.name AS reseller, b.name AS brand, rcat.raw_name AS cats_raw, cats1.name AS cat1, cats1.slug AS catslug1, cats2.name AS cat2, cats2.slug AS catslug2 FROM `jos_clothes_products` AS tbl LEFT JOIN `jos_clothes_brands` AS b ON b.clothes_brand_id = tbl.clothes_brand_id LEFT JOIN `jos_clothes_resellers` AS r ON r.clothes_reseller_id = tbl.clothes_reseller_id LEFT JOIN `jos_clothes_catraws` AS rcat ON rcat.clothes_catraw_id = tbl.clothes_catraw_id LEFT JOIN `jos_clothes_categories` AS cats2 ON cats2.clothes_category_id = rcat.clothes_category_id LEFT JOIN `jos_clothes_categories` AS cats1 ON cats1.clothes_category_id = cats2.parent_id </code></pre> <p>Then when running a query like this from PHP:</p> <pre><code>SELECT `tbl`.* FROM `jos_clothes_view_products` AS `tbl` WHERE `tbl`.`cat1` != 'NULL' AND `tbl`.`enabled` = '1' ORDER BY `created_on` DESC , `ordering` ASC LIMIT 0 , 20; </code></pre> <p>The query is often very slow! Not always though (probably because of caching?). I have also noticed that it creates a tmp-table at about 200MB in size. Sometimes it gets even bigger and fails with "Invalid key for file....".</p> <p>Any ideas how I can optimize the query? Or the VIEW actually, I guess it's the bottleneck here. Correct?</p> <p>Product table structure:</p> <pre><code>CREATE TABLE IF NOT EXISTS `jos_clothes_products` ( `clothes_product_id` bigint(20) unsigned NOT NULL auto_increment, `clothes_reseller_id` bigint(20) unsigned NOT NULL, `aff_prod_id` varchar(50) NOT NULL, `title` varchar(255) NOT NULL, `description` text NOT NULL, `gender` varchar(20) NOT NULL, `clothes_brand_id` bigint(20) unsigned NOT NULL, `color` varchar(255) NOT NULL, `size` varchar(50) NOT NULL, `clothes_catraw_id` bigint(20) unsigned NOT NULL, `price` decimal(10,2) NOT NULL default '0.00', `shipping_cost` varchar(20) NOT NULL default '0.00', `currency` varchar(10) NOT NULL, `availibility` tinyint(1) NOT NULL, `product_url` varchar(350) NOT NULL, `real_url` varchar(300) NOT NULL, `slug` varchar(255) NOT NULL, `hits` int(11) NOT NULL, `enabled` tinyint(1) NOT NULL default '0', `access` int(11) NOT NULL default '0', `ordering` bigint(20) unsigned NOT NULL, `created_on` datetime NOT NULL default '0000-00-00 00:00:00', `created_by` int(11) NOT NULL default '0', `modified_on` datetime NOT NULL default '0000-00-00 00:00:00', `modified_by` int(11) NOT NULL default '0', `locked_on` datetime NOT NULL default '0000-00-00 00:00:00', `locked_by` int(11) NOT NULL default '0', PRIMARY KEY (`clothes_product_id`), KEY `clothes_brand_id` (`clothes_brand_id`), KEY `clothes_catraw_id` (`clothes_catraw_id`), KEY `created_on` (`created_on`), KEY `clothes_reseller_id` (`clothes_reseller_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=18962 ; </code></pre> <p>Any ideas?</p> <p>Best Regards</p> <p>EDIT: Structure of jos_clothes_catraws</p> <pre><code>-- -- Struktur för tabell `jos_clothes_catraws` -- CREATE TABLE IF NOT EXISTS `jos_clothes_catraws` ( `clothes_catraw_id` int(11) unsigned NOT NULL auto_increment, `clothes_category_id` int(11) unsigned NOT NULL default '0', `clothes_reseller_id` int(11) unsigned NOT NULL, `raw_name` varchar(255) NOT NULL, PRIMARY KEY (`clothes_catraw_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1289 ; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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