Improve Query Performance of Select Statment inside IN Statment












1















My query is:



(3) UPDATE wp_postmeta set meta_value = 'outofstock' where meta_key = '_stock_status' and 
post_id in
(
(2)SELECT post_id FROM
(
(1) SELECT A.post_id from wp_postmeta A
JOIN wp_postmeta B ON A.post_id = B.post_id
AND A.meta_key = 'attribute_pa_beden'
and A.meta_value in ('12yas','34yas','56yas','78yas','910yas','1112yas')
and B.meta_key = 'attribute_pa_renk'
and ((B.meta_value = 'bordo') OR ('bordo' = 'hepsi'))
JOIN wp_posts ON A.post_id = wp_posts.id
JOIN wp_term_relationships ON wp_posts.post_parent = wp_term_relationships.object_id
and term_taxonomy_id in ('2643','2304')
) AS DerivedTable
)


To improve the speed of this query, I analysed it using the "Explain" statment. Below are the results:



When I added explain to (1) location in above query and run the subquery. The results are as below:



enter image description here



When I added explain to (2) location and run that subqyery, results are as below.



enter image description here



When I added explain to (3) location and run the whole query, results are as below:



enter image description here



My analysis is there is no speed problem with the (1) subquery, but after I select the data from this subquery to a derived table (2), somehow there is a 55.277.640 "rows" comes, and which seems to be the reason why my query is so slow. How can I optimize it ? What is wrong here ?



Edit: The tables are Wordpress WooCommerce module standart tables. I didn't modified them. Here SHOW CREATE TABLE results:



wp_postmeta



CREATE TABLE `wp_postmeta` (
`meta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`post_id` bigint(20) unsigned NOT NULL DEFAULT '0',
`meta_key` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`meta_value` longtext COLLATE utf8mb4_unicode_ci,
PRIMARY KEY (`meta_id`),
KEY `post_id` (`post_id`),
KEY `meta_key` (`meta_key`(191))
) ENGINE=MyISAM AUTO_INCREMENT=11119572 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci


wp_posts



CREATE TABLE `wp_posts` (
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`post_author` bigint(20) unsigned NOT NULL DEFAULT '0',
`post_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`post_date_gmt` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`post_content` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
`post_title` text COLLATE utf8mb4_unicode_ci NOT NULL,
`post_excerpt` text COLLATE utf8mb4_unicode_ci NOT NULL,
`post_status` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'publish',
`comment_status` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'open',
`ping_status` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'open',
`post_password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`post_name` varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`to_ping` text COLLATE utf8mb4_unicode_ci NOT NULL,
`pinged` text COLLATE utf8mb4_unicode_ci NOT NULL,
`post_modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`post_modified_gmt` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`post_content_filtered` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
`post_parent` bigint(20) unsigned NOT NULL DEFAULT '0',
`guid` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`menu_order` int(11) NOT NULL DEFAULT '0',
`post_type` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'post',
`post_mime_type` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`comment_count` bigint(20) NOT NULL DEFAULT '0',
PRIMARY KEY (`ID`),
KEY `post_name` (`post_name`(191)),
KEY `type_status_date` (`post_type`,`post_status`,`post_date`,`ID`),
KEY `post_parent` (`post_parent`),
KEY `post_author` (`post_author`)
) ENGINE=MyISAM AUTO_INCREMENT=352598 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci


wp_term_relationships



CREATE TABLE `wp_term_relationships` (
`object_id` bigint(20) unsigned NOT NULL DEFAULT '0',
`term_taxonomy_id` bigint(20) unsigned NOT NULL DEFAULT '0',
`term_order` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`object_id`,`term_taxonomy_id`),
KEY `term_taxonomy_id` (`term_taxonomy_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci









share|improve this question

























  • It won't affect performance bu that OR ('bordo' = 'hepsi') is useless and can be removed.

    – ypercubeᵀᴹ
    12 hours ago






  • 1





    It would help us if you posted the SHOW CREATE TABLE name; for all 3 tables in the query. And the version of MySQL.

    – ypercubeᵀᴹ
    12 hours ago













  • @ypercubeᵀᴹ updated the question with SHOW CREATE TABLE name; for the 3 tables in the query. They are all wordpress - woocommerce plugins standart tables.

    – HOY
    11 hours ago











  • Ewww, just noticed that the tables are MyISAM? Oh. my.

    – ypercubeᵀᴹ
    10 hours ago











  • OR ('bordo' = 'hepsi') -- What the heck?

    – Rick James
    10 hours ago
















1















My query is:



(3) UPDATE wp_postmeta set meta_value = 'outofstock' where meta_key = '_stock_status' and 
post_id in
(
(2)SELECT post_id FROM
(
(1) SELECT A.post_id from wp_postmeta A
JOIN wp_postmeta B ON A.post_id = B.post_id
AND A.meta_key = 'attribute_pa_beden'
and A.meta_value in ('12yas','34yas','56yas','78yas','910yas','1112yas')
and B.meta_key = 'attribute_pa_renk'
and ((B.meta_value = 'bordo') OR ('bordo' = 'hepsi'))
JOIN wp_posts ON A.post_id = wp_posts.id
JOIN wp_term_relationships ON wp_posts.post_parent = wp_term_relationships.object_id
and term_taxonomy_id in ('2643','2304')
) AS DerivedTable
)


To improve the speed of this query, I analysed it using the "Explain" statment. Below are the results:



When I added explain to (1) location in above query and run the subquery. The results are as below:



enter image description here



When I added explain to (2) location and run that subqyery, results are as below.



enter image description here



When I added explain to (3) location and run the whole query, results are as below:



enter image description here



My analysis is there is no speed problem with the (1) subquery, but after I select the data from this subquery to a derived table (2), somehow there is a 55.277.640 "rows" comes, and which seems to be the reason why my query is so slow. How can I optimize it ? What is wrong here ?



Edit: The tables are Wordpress WooCommerce module standart tables. I didn't modified them. Here SHOW CREATE TABLE results:



wp_postmeta



CREATE TABLE `wp_postmeta` (
`meta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`post_id` bigint(20) unsigned NOT NULL DEFAULT '0',
`meta_key` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`meta_value` longtext COLLATE utf8mb4_unicode_ci,
PRIMARY KEY (`meta_id`),
KEY `post_id` (`post_id`),
KEY `meta_key` (`meta_key`(191))
) ENGINE=MyISAM AUTO_INCREMENT=11119572 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci


wp_posts



CREATE TABLE `wp_posts` (
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`post_author` bigint(20) unsigned NOT NULL DEFAULT '0',
`post_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`post_date_gmt` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`post_content` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
`post_title` text COLLATE utf8mb4_unicode_ci NOT NULL,
`post_excerpt` text COLLATE utf8mb4_unicode_ci NOT NULL,
`post_status` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'publish',
`comment_status` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'open',
`ping_status` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'open',
`post_password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`post_name` varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`to_ping` text COLLATE utf8mb4_unicode_ci NOT NULL,
`pinged` text COLLATE utf8mb4_unicode_ci NOT NULL,
`post_modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`post_modified_gmt` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`post_content_filtered` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
`post_parent` bigint(20) unsigned NOT NULL DEFAULT '0',
`guid` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`menu_order` int(11) NOT NULL DEFAULT '0',
`post_type` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'post',
`post_mime_type` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`comment_count` bigint(20) NOT NULL DEFAULT '0',
PRIMARY KEY (`ID`),
KEY `post_name` (`post_name`(191)),
KEY `type_status_date` (`post_type`,`post_status`,`post_date`,`ID`),
KEY `post_parent` (`post_parent`),
KEY `post_author` (`post_author`)
) ENGINE=MyISAM AUTO_INCREMENT=352598 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci


wp_term_relationships



CREATE TABLE `wp_term_relationships` (
`object_id` bigint(20) unsigned NOT NULL DEFAULT '0',
`term_taxonomy_id` bigint(20) unsigned NOT NULL DEFAULT '0',
`term_order` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`object_id`,`term_taxonomy_id`),
KEY `term_taxonomy_id` (`term_taxonomy_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci









share|improve this question

























  • It won't affect performance bu that OR ('bordo' = 'hepsi') is useless and can be removed.

    – ypercubeᵀᴹ
    12 hours ago






  • 1





    It would help us if you posted the SHOW CREATE TABLE name; for all 3 tables in the query. And the version of MySQL.

    – ypercubeᵀᴹ
    12 hours ago













  • @ypercubeᵀᴹ updated the question with SHOW CREATE TABLE name; for the 3 tables in the query. They are all wordpress - woocommerce plugins standart tables.

    – HOY
    11 hours ago











  • Ewww, just noticed that the tables are MyISAM? Oh. my.

    – ypercubeᵀᴹ
    10 hours ago











  • OR ('bordo' = 'hepsi') -- What the heck?

    – Rick James
    10 hours ago














1












1








1








My query is:



(3) UPDATE wp_postmeta set meta_value = 'outofstock' where meta_key = '_stock_status' and 
post_id in
(
(2)SELECT post_id FROM
(
(1) SELECT A.post_id from wp_postmeta A
JOIN wp_postmeta B ON A.post_id = B.post_id
AND A.meta_key = 'attribute_pa_beden'
and A.meta_value in ('12yas','34yas','56yas','78yas','910yas','1112yas')
and B.meta_key = 'attribute_pa_renk'
and ((B.meta_value = 'bordo') OR ('bordo' = 'hepsi'))
JOIN wp_posts ON A.post_id = wp_posts.id
JOIN wp_term_relationships ON wp_posts.post_parent = wp_term_relationships.object_id
and term_taxonomy_id in ('2643','2304')
) AS DerivedTable
)


To improve the speed of this query, I analysed it using the "Explain" statment. Below are the results:



When I added explain to (1) location in above query and run the subquery. The results are as below:



enter image description here



When I added explain to (2) location and run that subqyery, results are as below.



enter image description here



When I added explain to (3) location and run the whole query, results are as below:



enter image description here



My analysis is there is no speed problem with the (1) subquery, but after I select the data from this subquery to a derived table (2), somehow there is a 55.277.640 "rows" comes, and which seems to be the reason why my query is so slow. How can I optimize it ? What is wrong here ?



Edit: The tables are Wordpress WooCommerce module standart tables. I didn't modified them. Here SHOW CREATE TABLE results:



wp_postmeta



CREATE TABLE `wp_postmeta` (
`meta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`post_id` bigint(20) unsigned NOT NULL DEFAULT '0',
`meta_key` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`meta_value` longtext COLLATE utf8mb4_unicode_ci,
PRIMARY KEY (`meta_id`),
KEY `post_id` (`post_id`),
KEY `meta_key` (`meta_key`(191))
) ENGINE=MyISAM AUTO_INCREMENT=11119572 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci


wp_posts



CREATE TABLE `wp_posts` (
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`post_author` bigint(20) unsigned NOT NULL DEFAULT '0',
`post_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`post_date_gmt` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`post_content` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
`post_title` text COLLATE utf8mb4_unicode_ci NOT NULL,
`post_excerpt` text COLLATE utf8mb4_unicode_ci NOT NULL,
`post_status` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'publish',
`comment_status` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'open',
`ping_status` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'open',
`post_password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`post_name` varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`to_ping` text COLLATE utf8mb4_unicode_ci NOT NULL,
`pinged` text COLLATE utf8mb4_unicode_ci NOT NULL,
`post_modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`post_modified_gmt` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`post_content_filtered` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
`post_parent` bigint(20) unsigned NOT NULL DEFAULT '0',
`guid` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`menu_order` int(11) NOT NULL DEFAULT '0',
`post_type` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'post',
`post_mime_type` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`comment_count` bigint(20) NOT NULL DEFAULT '0',
PRIMARY KEY (`ID`),
KEY `post_name` (`post_name`(191)),
KEY `type_status_date` (`post_type`,`post_status`,`post_date`,`ID`),
KEY `post_parent` (`post_parent`),
KEY `post_author` (`post_author`)
) ENGINE=MyISAM AUTO_INCREMENT=352598 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci


wp_term_relationships



CREATE TABLE `wp_term_relationships` (
`object_id` bigint(20) unsigned NOT NULL DEFAULT '0',
`term_taxonomy_id` bigint(20) unsigned NOT NULL DEFAULT '0',
`term_order` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`object_id`,`term_taxonomy_id`),
KEY `term_taxonomy_id` (`term_taxonomy_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci









share|improve this question
















My query is:



(3) UPDATE wp_postmeta set meta_value = 'outofstock' where meta_key = '_stock_status' and 
post_id in
(
(2)SELECT post_id FROM
(
(1) SELECT A.post_id from wp_postmeta A
JOIN wp_postmeta B ON A.post_id = B.post_id
AND A.meta_key = 'attribute_pa_beden'
and A.meta_value in ('12yas','34yas','56yas','78yas','910yas','1112yas')
and B.meta_key = 'attribute_pa_renk'
and ((B.meta_value = 'bordo') OR ('bordo' = 'hepsi'))
JOIN wp_posts ON A.post_id = wp_posts.id
JOIN wp_term_relationships ON wp_posts.post_parent = wp_term_relationships.object_id
and term_taxonomy_id in ('2643','2304')
) AS DerivedTable
)


To improve the speed of this query, I analysed it using the "Explain" statment. Below are the results:



When I added explain to (1) location in above query and run the subquery. The results are as below:



enter image description here



When I added explain to (2) location and run that subqyery, results are as below.



enter image description here



When I added explain to (3) location and run the whole query, results are as below:



enter image description here



My analysis is there is no speed problem with the (1) subquery, but after I select the data from this subquery to a derived table (2), somehow there is a 55.277.640 "rows" comes, and which seems to be the reason why my query is so slow. How can I optimize it ? What is wrong here ?



Edit: The tables are Wordpress WooCommerce module standart tables. I didn't modified them. Here SHOW CREATE TABLE results:



wp_postmeta



CREATE TABLE `wp_postmeta` (
`meta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`post_id` bigint(20) unsigned NOT NULL DEFAULT '0',
`meta_key` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`meta_value` longtext COLLATE utf8mb4_unicode_ci,
PRIMARY KEY (`meta_id`),
KEY `post_id` (`post_id`),
KEY `meta_key` (`meta_key`(191))
) ENGINE=MyISAM AUTO_INCREMENT=11119572 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci


wp_posts



CREATE TABLE `wp_posts` (
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`post_author` bigint(20) unsigned NOT NULL DEFAULT '0',
`post_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`post_date_gmt` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`post_content` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
`post_title` text COLLATE utf8mb4_unicode_ci NOT NULL,
`post_excerpt` text COLLATE utf8mb4_unicode_ci NOT NULL,
`post_status` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'publish',
`comment_status` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'open',
`ping_status` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'open',
`post_password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`post_name` varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`to_ping` text COLLATE utf8mb4_unicode_ci NOT NULL,
`pinged` text COLLATE utf8mb4_unicode_ci NOT NULL,
`post_modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`post_modified_gmt` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`post_content_filtered` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
`post_parent` bigint(20) unsigned NOT NULL DEFAULT '0',
`guid` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`menu_order` int(11) NOT NULL DEFAULT '0',
`post_type` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'post',
`post_mime_type` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`comment_count` bigint(20) NOT NULL DEFAULT '0',
PRIMARY KEY (`ID`),
KEY `post_name` (`post_name`(191)),
KEY `type_status_date` (`post_type`,`post_status`,`post_date`,`ID`),
KEY `post_parent` (`post_parent`),
KEY `post_author` (`post_author`)
) ENGINE=MyISAM AUTO_INCREMENT=352598 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci


wp_term_relationships



CREATE TABLE `wp_term_relationships` (
`object_id` bigint(20) unsigned NOT NULL DEFAULT '0',
`term_taxonomy_id` bigint(20) unsigned NOT NULL DEFAULT '0',
`term_order` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`object_id`,`term_taxonomy_id`),
KEY `term_taxonomy_id` (`term_taxonomy_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci






mysql query-performance optimization select derived-tables






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 11 hours ago







HOY

















asked 12 hours ago









HOYHOY

1117




1117













  • It won't affect performance bu that OR ('bordo' = 'hepsi') is useless and can be removed.

    – ypercubeᵀᴹ
    12 hours ago






  • 1





    It would help us if you posted the SHOW CREATE TABLE name; for all 3 tables in the query. And the version of MySQL.

    – ypercubeᵀᴹ
    12 hours ago













  • @ypercubeᵀᴹ updated the question with SHOW CREATE TABLE name; for the 3 tables in the query. They are all wordpress - woocommerce plugins standart tables.

    – HOY
    11 hours ago











  • Ewww, just noticed that the tables are MyISAM? Oh. my.

    – ypercubeᵀᴹ
    10 hours ago











  • OR ('bordo' = 'hepsi') -- What the heck?

    – Rick James
    10 hours ago



















  • It won't affect performance bu that OR ('bordo' = 'hepsi') is useless and can be removed.

    – ypercubeᵀᴹ
    12 hours ago






  • 1





    It would help us if you posted the SHOW CREATE TABLE name; for all 3 tables in the query. And the version of MySQL.

    – ypercubeᵀᴹ
    12 hours ago













  • @ypercubeᵀᴹ updated the question with SHOW CREATE TABLE name; for the 3 tables in the query. They are all wordpress - woocommerce plugins standart tables.

    – HOY
    11 hours ago











  • Ewww, just noticed that the tables are MyISAM? Oh. my.

    – ypercubeᵀᴹ
    10 hours ago











  • OR ('bordo' = 'hepsi') -- What the heck?

    – Rick James
    10 hours ago

















It won't affect performance bu that OR ('bordo' = 'hepsi') is useless and can be removed.

– ypercubeᵀᴹ
12 hours ago





It won't affect performance bu that OR ('bordo' = 'hepsi') is useless and can be removed.

– ypercubeᵀᴹ
12 hours ago




1




1





It would help us if you posted the SHOW CREATE TABLE name; for all 3 tables in the query. And the version of MySQL.

– ypercubeᵀᴹ
12 hours ago







It would help us if you posted the SHOW CREATE TABLE name; for all 3 tables in the query. And the version of MySQL.

– ypercubeᵀᴹ
12 hours ago















@ypercubeᵀᴹ updated the question with SHOW CREATE TABLE name; for the 3 tables in the query. They are all wordpress - woocommerce plugins standart tables.

– HOY
11 hours ago





@ypercubeᵀᴹ updated the question with SHOW CREATE TABLE name; for the 3 tables in the query. They are all wordpress - woocommerce plugins standart tables.

– HOY
11 hours ago













Ewww, just noticed that the tables are MyISAM? Oh. my.

– ypercubeᵀᴹ
10 hours ago





Ewww, just noticed that the tables are MyISAM? Oh. my.

– ypercubeᵀᴹ
10 hours ago













OR ('bordo' = 'hepsi') -- What the heck?

– Rick James
10 hours ago





OR ('bordo' = 'hepsi') -- What the heck?

– Rick James
10 hours ago










3 Answers
3






active

oldest

votes


















2














postmeta has inefficient. See https://stackoverflow.com/questions/43859351/why-are-references-to-wp-postmeta-so-slow
for a discussion of what to do about it. Or: http://mysql.rjweb.org/doc.php/index_cookbook_mysql#speeding_up_wp_postmeta or
https://wordpress.stackexchange.com/questions/248207/simple-sql-query-on-wp-postmeta-very-slow






share|improve this answer
























  • Hi Rick, I am happy to see you. Actually you helped me for the same issue on a previous [topic] (stackoverflow.com/questions/53854249/…). In which you told me that "In some situations, IN ( SELECT ... ) is much slower because it reevaluates the SELECT for every post_id". This is why I thought that this is something related to my SQL syntax rather than the postmeta itself. I checked the first link you shared. They seems to be helpful, but does it really removes the problem of IN (SELECT...) problem that I am suffering ?

    – HOY
    10 hours ago











  • Are there really 5.5M rows and 227K rows for the intermediate results? If so, my postmeta tips will speed it up, but you may have a much bigger problem -- namely why do you need to update 227K rows??

    – Rick James
    10 hours ago











  • @HOY - And... Even if you get past the inefficiencies of the in-select and postmeta, the cost of updating 227K rows is high. This is because of the need to capture the previous copy of each row incase of a crash and rollback.

    – Rick James
    10 hours ago











  • @RickJames Rollback? The tables are MyiSAM ...

    – ypercubeᵀᴹ
    10 hours ago











  • Ouch. If you crash in the middle of updating 227K rows, some of the rows will be changed; you won't know which. This is one of several big reasons to move to InnoDB.

    – Rick James
    10 hours ago



















1














First, the Wordpress database design has several flaws as Rick James points out in his answer and the linked ones. The wp_postmeta in particular is a common cause of performance problems in many WP installations, as soon they grow enough and have more than a few thousands rows.



Before going the difficult and long road of addressing that, I'll suggest something that might help improving the specific query in the short time:





Rewrite the IN (complex subquery) to a JOIN:



UPDATE
(
SELECT A.post_id from wp_postmeta A
JOIN wp_postmeta B ON A.post_id = B.post_id
AND A.meta_key = 'attribute_pa_beden'
and A.meta_value in ('12yas','34yas','56yas','78yas','910yas','1112yas')
and B.meta_key = 'attribute_pa_renk'
and ((B.meta_value = 'bordo') OR ('bordo' = 'hepsi'))
JOIN wp_posts ON A.post_id = wp_posts.id
JOIN wp_term_relationships ON wp_posts.post_parent = wp_term_relationships.object_id
and term_taxonomy_id in ('2643','2304')
) AS DerivedTable
JOIN
wp_postmeta AS upd
ON
upd.post_id = DerivedTable.post_id
SET
upd.meta_value = 'outofstock'
WHERE
upd.meta_key = '_stock_status' ;


and add an index on wp_postmeta (meta_key(191), post_id)






share|improve this answer


























  • It is useless to have any columns after a prefix; the rest will be ignored. Think of it this way -- a prefix is essentially a 'range'.

    – Rick James
    10 hours ago











  • I wanted to suggest a (meta_key, post_id) but the VARCHAR(255) utf8mb4 does not allow it. Correct?

    – ypercubeᵀᴹ
    10 hours ago











  • Yep. And I give five workarounds here; none is perfect.

    – Rick James
    10 hours ago



















0














While what I am proposing is not necessarily the best idea in the general case, I think for this specific situation you would be better off using nested IN clauses rather than the complicated self-join. Try this:



  UPDATE wp_postmeta set meta_value = 'outofstock' where meta_key = '_stock_status' and 
post_id in
(
SELECT B.post_id FROM wp_postmeta B where B.meta_key = 'attribute_pa_renk'
and ((B.meta_value = 'bordo') OR ('bordo' = 'hepsi'))
and B.post_id in
(
SELECT A.post_id from wp_postmeta A
JOIN wp_posts ON A.post_id = wp_posts.id
JOIN wp_term_relationships
ON wp_posts.post_parent = wp_term_relationships.object_id
and term_taxonomy_id in ('2643','2304')
WHERE A.meta_key = 'attribute_pa_beden'
and A.meta_value in
('12yas','34yas','56yas','78yas','910yas','1112yas')

)
)


And also, in wp_postmeta, replace



 KEY `post_id` (`post_id`),


with



 KEY `post_id` (`post_id`, `meta_key`(191)),





share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "182"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f229348%2fimprove-query-performance-of-select-statment-inside-in-statment%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    postmeta has inefficient. See https://stackoverflow.com/questions/43859351/why-are-references-to-wp-postmeta-so-slow
    for a discussion of what to do about it. Or: http://mysql.rjweb.org/doc.php/index_cookbook_mysql#speeding_up_wp_postmeta or
    https://wordpress.stackexchange.com/questions/248207/simple-sql-query-on-wp-postmeta-very-slow






    share|improve this answer
























    • Hi Rick, I am happy to see you. Actually you helped me for the same issue on a previous [topic] (stackoverflow.com/questions/53854249/…). In which you told me that "In some situations, IN ( SELECT ... ) is much slower because it reevaluates the SELECT for every post_id". This is why I thought that this is something related to my SQL syntax rather than the postmeta itself. I checked the first link you shared. They seems to be helpful, but does it really removes the problem of IN (SELECT...) problem that I am suffering ?

      – HOY
      10 hours ago











    • Are there really 5.5M rows and 227K rows for the intermediate results? If so, my postmeta tips will speed it up, but you may have a much bigger problem -- namely why do you need to update 227K rows??

      – Rick James
      10 hours ago











    • @HOY - And... Even if you get past the inefficiencies of the in-select and postmeta, the cost of updating 227K rows is high. This is because of the need to capture the previous copy of each row incase of a crash and rollback.

      – Rick James
      10 hours ago











    • @RickJames Rollback? The tables are MyiSAM ...

      – ypercubeᵀᴹ
      10 hours ago











    • Ouch. If you crash in the middle of updating 227K rows, some of the rows will be changed; you won't know which. This is one of several big reasons to move to InnoDB.

      – Rick James
      10 hours ago
















    2














    postmeta has inefficient. See https://stackoverflow.com/questions/43859351/why-are-references-to-wp-postmeta-so-slow
    for a discussion of what to do about it. Or: http://mysql.rjweb.org/doc.php/index_cookbook_mysql#speeding_up_wp_postmeta or
    https://wordpress.stackexchange.com/questions/248207/simple-sql-query-on-wp-postmeta-very-slow






    share|improve this answer
























    • Hi Rick, I am happy to see you. Actually you helped me for the same issue on a previous [topic] (stackoverflow.com/questions/53854249/…). In which you told me that "In some situations, IN ( SELECT ... ) is much slower because it reevaluates the SELECT for every post_id". This is why I thought that this is something related to my SQL syntax rather than the postmeta itself. I checked the first link you shared. They seems to be helpful, but does it really removes the problem of IN (SELECT...) problem that I am suffering ?

      – HOY
      10 hours ago











    • Are there really 5.5M rows and 227K rows for the intermediate results? If so, my postmeta tips will speed it up, but you may have a much bigger problem -- namely why do you need to update 227K rows??

      – Rick James
      10 hours ago











    • @HOY - And... Even if you get past the inefficiencies of the in-select and postmeta, the cost of updating 227K rows is high. This is because of the need to capture the previous copy of each row incase of a crash and rollback.

      – Rick James
      10 hours ago











    • @RickJames Rollback? The tables are MyiSAM ...

      – ypercubeᵀᴹ
      10 hours ago











    • Ouch. If you crash in the middle of updating 227K rows, some of the rows will be changed; you won't know which. This is one of several big reasons to move to InnoDB.

      – Rick James
      10 hours ago














    2












    2








    2







    postmeta has inefficient. See https://stackoverflow.com/questions/43859351/why-are-references-to-wp-postmeta-so-slow
    for a discussion of what to do about it. Or: http://mysql.rjweb.org/doc.php/index_cookbook_mysql#speeding_up_wp_postmeta or
    https://wordpress.stackexchange.com/questions/248207/simple-sql-query-on-wp-postmeta-very-slow






    share|improve this answer













    postmeta has inefficient. See https://stackoverflow.com/questions/43859351/why-are-references-to-wp-postmeta-so-slow
    for a discussion of what to do about it. Or: http://mysql.rjweb.org/doc.php/index_cookbook_mysql#speeding_up_wp_postmeta or
    https://wordpress.stackexchange.com/questions/248207/simple-sql-query-on-wp-postmeta-very-slow







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 11 hours ago









    Rick JamesRick James

    42.5k22258




    42.5k22258













    • Hi Rick, I am happy to see you. Actually you helped me for the same issue on a previous [topic] (stackoverflow.com/questions/53854249/…). In which you told me that "In some situations, IN ( SELECT ... ) is much slower because it reevaluates the SELECT for every post_id". This is why I thought that this is something related to my SQL syntax rather than the postmeta itself. I checked the first link you shared. They seems to be helpful, but does it really removes the problem of IN (SELECT...) problem that I am suffering ?

      – HOY
      10 hours ago











    • Are there really 5.5M rows and 227K rows for the intermediate results? If so, my postmeta tips will speed it up, but you may have a much bigger problem -- namely why do you need to update 227K rows??

      – Rick James
      10 hours ago











    • @HOY - And... Even if you get past the inefficiencies of the in-select and postmeta, the cost of updating 227K rows is high. This is because of the need to capture the previous copy of each row incase of a crash and rollback.

      – Rick James
      10 hours ago











    • @RickJames Rollback? The tables are MyiSAM ...

      – ypercubeᵀᴹ
      10 hours ago











    • Ouch. If you crash in the middle of updating 227K rows, some of the rows will be changed; you won't know which. This is one of several big reasons to move to InnoDB.

      – Rick James
      10 hours ago



















    • Hi Rick, I am happy to see you. Actually you helped me for the same issue on a previous [topic] (stackoverflow.com/questions/53854249/…). In which you told me that "In some situations, IN ( SELECT ... ) is much slower because it reevaluates the SELECT for every post_id". This is why I thought that this is something related to my SQL syntax rather than the postmeta itself. I checked the first link you shared. They seems to be helpful, but does it really removes the problem of IN (SELECT...) problem that I am suffering ?

      – HOY
      10 hours ago











    • Are there really 5.5M rows and 227K rows for the intermediate results? If so, my postmeta tips will speed it up, but you may have a much bigger problem -- namely why do you need to update 227K rows??

      – Rick James
      10 hours ago











    • @HOY - And... Even if you get past the inefficiencies of the in-select and postmeta, the cost of updating 227K rows is high. This is because of the need to capture the previous copy of each row incase of a crash and rollback.

      – Rick James
      10 hours ago











    • @RickJames Rollback? The tables are MyiSAM ...

      – ypercubeᵀᴹ
      10 hours ago











    • Ouch. If you crash in the middle of updating 227K rows, some of the rows will be changed; you won't know which. This is one of several big reasons to move to InnoDB.

      – Rick James
      10 hours ago

















    Hi Rick, I am happy to see you. Actually you helped me for the same issue on a previous [topic] (stackoverflow.com/questions/53854249/…). In which you told me that "In some situations, IN ( SELECT ... ) is much slower because it reevaluates the SELECT for every post_id". This is why I thought that this is something related to my SQL syntax rather than the postmeta itself. I checked the first link you shared. They seems to be helpful, but does it really removes the problem of IN (SELECT...) problem that I am suffering ?

    – HOY
    10 hours ago





    Hi Rick, I am happy to see you. Actually you helped me for the same issue on a previous [topic] (stackoverflow.com/questions/53854249/…). In which you told me that "In some situations, IN ( SELECT ... ) is much slower because it reevaluates the SELECT for every post_id". This is why I thought that this is something related to my SQL syntax rather than the postmeta itself. I checked the first link you shared. They seems to be helpful, but does it really removes the problem of IN (SELECT...) problem that I am suffering ?

    – HOY
    10 hours ago













    Are there really 5.5M rows and 227K rows for the intermediate results? If so, my postmeta tips will speed it up, but you may have a much bigger problem -- namely why do you need to update 227K rows??

    – Rick James
    10 hours ago





    Are there really 5.5M rows and 227K rows for the intermediate results? If so, my postmeta tips will speed it up, but you may have a much bigger problem -- namely why do you need to update 227K rows??

    – Rick James
    10 hours ago













    @HOY - And... Even if you get past the inefficiencies of the in-select and postmeta, the cost of updating 227K rows is high. This is because of the need to capture the previous copy of each row incase of a crash and rollback.

    – Rick James
    10 hours ago





    @HOY - And... Even if you get past the inefficiencies of the in-select and postmeta, the cost of updating 227K rows is high. This is because of the need to capture the previous copy of each row incase of a crash and rollback.

    – Rick James
    10 hours ago













    @RickJames Rollback? The tables are MyiSAM ...

    – ypercubeᵀᴹ
    10 hours ago





    @RickJames Rollback? The tables are MyiSAM ...

    – ypercubeᵀᴹ
    10 hours ago













    Ouch. If you crash in the middle of updating 227K rows, some of the rows will be changed; you won't know which. This is one of several big reasons to move to InnoDB.

    – Rick James
    10 hours ago





    Ouch. If you crash in the middle of updating 227K rows, some of the rows will be changed; you won't know which. This is one of several big reasons to move to InnoDB.

    – Rick James
    10 hours ago













    1














    First, the Wordpress database design has several flaws as Rick James points out in his answer and the linked ones. The wp_postmeta in particular is a common cause of performance problems in many WP installations, as soon they grow enough and have more than a few thousands rows.



    Before going the difficult and long road of addressing that, I'll suggest something that might help improving the specific query in the short time:





    Rewrite the IN (complex subquery) to a JOIN:



    UPDATE
    (
    SELECT A.post_id from wp_postmeta A
    JOIN wp_postmeta B ON A.post_id = B.post_id
    AND A.meta_key = 'attribute_pa_beden'
    and A.meta_value in ('12yas','34yas','56yas','78yas','910yas','1112yas')
    and B.meta_key = 'attribute_pa_renk'
    and ((B.meta_value = 'bordo') OR ('bordo' = 'hepsi'))
    JOIN wp_posts ON A.post_id = wp_posts.id
    JOIN wp_term_relationships ON wp_posts.post_parent = wp_term_relationships.object_id
    and term_taxonomy_id in ('2643','2304')
    ) AS DerivedTable
    JOIN
    wp_postmeta AS upd
    ON
    upd.post_id = DerivedTable.post_id
    SET
    upd.meta_value = 'outofstock'
    WHERE
    upd.meta_key = '_stock_status' ;


    and add an index on wp_postmeta (meta_key(191), post_id)






    share|improve this answer


























    • It is useless to have any columns after a prefix; the rest will be ignored. Think of it this way -- a prefix is essentially a 'range'.

      – Rick James
      10 hours ago











    • I wanted to suggest a (meta_key, post_id) but the VARCHAR(255) utf8mb4 does not allow it. Correct?

      – ypercubeᵀᴹ
      10 hours ago











    • Yep. And I give five workarounds here; none is perfect.

      – Rick James
      10 hours ago
















    1














    First, the Wordpress database design has several flaws as Rick James points out in his answer and the linked ones. The wp_postmeta in particular is a common cause of performance problems in many WP installations, as soon they grow enough and have more than a few thousands rows.



    Before going the difficult and long road of addressing that, I'll suggest something that might help improving the specific query in the short time:





    Rewrite the IN (complex subquery) to a JOIN:



    UPDATE
    (
    SELECT A.post_id from wp_postmeta A
    JOIN wp_postmeta B ON A.post_id = B.post_id
    AND A.meta_key = 'attribute_pa_beden'
    and A.meta_value in ('12yas','34yas','56yas','78yas','910yas','1112yas')
    and B.meta_key = 'attribute_pa_renk'
    and ((B.meta_value = 'bordo') OR ('bordo' = 'hepsi'))
    JOIN wp_posts ON A.post_id = wp_posts.id
    JOIN wp_term_relationships ON wp_posts.post_parent = wp_term_relationships.object_id
    and term_taxonomy_id in ('2643','2304')
    ) AS DerivedTable
    JOIN
    wp_postmeta AS upd
    ON
    upd.post_id = DerivedTable.post_id
    SET
    upd.meta_value = 'outofstock'
    WHERE
    upd.meta_key = '_stock_status' ;


    and add an index on wp_postmeta (meta_key(191), post_id)






    share|improve this answer


























    • It is useless to have any columns after a prefix; the rest will be ignored. Think of it this way -- a prefix is essentially a 'range'.

      – Rick James
      10 hours ago











    • I wanted to suggest a (meta_key, post_id) but the VARCHAR(255) utf8mb4 does not allow it. Correct?

      – ypercubeᵀᴹ
      10 hours ago











    • Yep. And I give five workarounds here; none is perfect.

      – Rick James
      10 hours ago














    1












    1








    1







    First, the Wordpress database design has several flaws as Rick James points out in his answer and the linked ones. The wp_postmeta in particular is a common cause of performance problems in many WP installations, as soon they grow enough and have more than a few thousands rows.



    Before going the difficult and long road of addressing that, I'll suggest something that might help improving the specific query in the short time:





    Rewrite the IN (complex subquery) to a JOIN:



    UPDATE
    (
    SELECT A.post_id from wp_postmeta A
    JOIN wp_postmeta B ON A.post_id = B.post_id
    AND A.meta_key = 'attribute_pa_beden'
    and A.meta_value in ('12yas','34yas','56yas','78yas','910yas','1112yas')
    and B.meta_key = 'attribute_pa_renk'
    and ((B.meta_value = 'bordo') OR ('bordo' = 'hepsi'))
    JOIN wp_posts ON A.post_id = wp_posts.id
    JOIN wp_term_relationships ON wp_posts.post_parent = wp_term_relationships.object_id
    and term_taxonomy_id in ('2643','2304')
    ) AS DerivedTable
    JOIN
    wp_postmeta AS upd
    ON
    upd.post_id = DerivedTable.post_id
    SET
    upd.meta_value = 'outofstock'
    WHERE
    upd.meta_key = '_stock_status' ;


    and add an index on wp_postmeta (meta_key(191), post_id)






    share|improve this answer















    First, the Wordpress database design has several flaws as Rick James points out in his answer and the linked ones. The wp_postmeta in particular is a common cause of performance problems in many WP installations, as soon they grow enough and have more than a few thousands rows.



    Before going the difficult and long road of addressing that, I'll suggest something that might help improving the specific query in the short time:





    Rewrite the IN (complex subquery) to a JOIN:



    UPDATE
    (
    SELECT A.post_id from wp_postmeta A
    JOIN wp_postmeta B ON A.post_id = B.post_id
    AND A.meta_key = 'attribute_pa_beden'
    and A.meta_value in ('12yas','34yas','56yas','78yas','910yas','1112yas')
    and B.meta_key = 'attribute_pa_renk'
    and ((B.meta_value = 'bordo') OR ('bordo' = 'hepsi'))
    JOIN wp_posts ON A.post_id = wp_posts.id
    JOIN wp_term_relationships ON wp_posts.post_parent = wp_term_relationships.object_id
    and term_taxonomy_id in ('2643','2304')
    ) AS DerivedTable
    JOIN
    wp_postmeta AS upd
    ON
    upd.post_id = DerivedTable.post_id
    SET
    upd.meta_value = 'outofstock'
    WHERE
    upd.meta_key = '_stock_status' ;


    and add an index on wp_postmeta (meta_key(191), post_id)







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 10 hours ago

























    answered 10 hours ago









    ypercubeᵀᴹypercubeᵀᴹ

    76k11130212




    76k11130212













    • It is useless to have any columns after a prefix; the rest will be ignored. Think of it this way -- a prefix is essentially a 'range'.

      – Rick James
      10 hours ago











    • I wanted to suggest a (meta_key, post_id) but the VARCHAR(255) utf8mb4 does not allow it. Correct?

      – ypercubeᵀᴹ
      10 hours ago











    • Yep. And I give five workarounds here; none is perfect.

      – Rick James
      10 hours ago



















    • It is useless to have any columns after a prefix; the rest will be ignored. Think of it this way -- a prefix is essentially a 'range'.

      – Rick James
      10 hours ago











    • I wanted to suggest a (meta_key, post_id) but the VARCHAR(255) utf8mb4 does not allow it. Correct?

      – ypercubeᵀᴹ
      10 hours ago











    • Yep. And I give five workarounds here; none is perfect.

      – Rick James
      10 hours ago

















    It is useless to have any columns after a prefix; the rest will be ignored. Think of it this way -- a prefix is essentially a 'range'.

    – Rick James
    10 hours ago





    It is useless to have any columns after a prefix; the rest will be ignored. Think of it this way -- a prefix is essentially a 'range'.

    – Rick James
    10 hours ago













    I wanted to suggest a (meta_key, post_id) but the VARCHAR(255) utf8mb4 does not allow it. Correct?

    – ypercubeᵀᴹ
    10 hours ago





    I wanted to suggest a (meta_key, post_id) but the VARCHAR(255) utf8mb4 does not allow it. Correct?

    – ypercubeᵀᴹ
    10 hours ago













    Yep. And I give five workarounds here; none is perfect.

    – Rick James
    10 hours ago





    Yep. And I give five workarounds here; none is perfect.

    – Rick James
    10 hours ago











    0














    While what I am proposing is not necessarily the best idea in the general case, I think for this specific situation you would be better off using nested IN clauses rather than the complicated self-join. Try this:



      UPDATE wp_postmeta set meta_value = 'outofstock' where meta_key = '_stock_status' and 
    post_id in
    (
    SELECT B.post_id FROM wp_postmeta B where B.meta_key = 'attribute_pa_renk'
    and ((B.meta_value = 'bordo') OR ('bordo' = 'hepsi'))
    and B.post_id in
    (
    SELECT A.post_id from wp_postmeta A
    JOIN wp_posts ON A.post_id = wp_posts.id
    JOIN wp_term_relationships
    ON wp_posts.post_parent = wp_term_relationships.object_id
    and term_taxonomy_id in ('2643','2304')
    WHERE A.meta_key = 'attribute_pa_beden'
    and A.meta_value in
    ('12yas','34yas','56yas','78yas','910yas','1112yas')

    )
    )


    And also, in wp_postmeta, replace



     KEY `post_id` (`post_id`),


    with



     KEY `post_id` (`post_id`, `meta_key`(191)),





    share|improve this answer




























      0














      While what I am proposing is not necessarily the best idea in the general case, I think for this specific situation you would be better off using nested IN clauses rather than the complicated self-join. Try this:



        UPDATE wp_postmeta set meta_value = 'outofstock' where meta_key = '_stock_status' and 
      post_id in
      (
      SELECT B.post_id FROM wp_postmeta B where B.meta_key = 'attribute_pa_renk'
      and ((B.meta_value = 'bordo') OR ('bordo' = 'hepsi'))
      and B.post_id in
      (
      SELECT A.post_id from wp_postmeta A
      JOIN wp_posts ON A.post_id = wp_posts.id
      JOIN wp_term_relationships
      ON wp_posts.post_parent = wp_term_relationships.object_id
      and term_taxonomy_id in ('2643','2304')
      WHERE A.meta_key = 'attribute_pa_beden'
      and A.meta_value in
      ('12yas','34yas','56yas','78yas','910yas','1112yas')

      )
      )


      And also, in wp_postmeta, replace



       KEY `post_id` (`post_id`),


      with



       KEY `post_id` (`post_id`, `meta_key`(191)),





      share|improve this answer


























        0












        0








        0







        While what I am proposing is not necessarily the best idea in the general case, I think for this specific situation you would be better off using nested IN clauses rather than the complicated self-join. Try this:



          UPDATE wp_postmeta set meta_value = 'outofstock' where meta_key = '_stock_status' and 
        post_id in
        (
        SELECT B.post_id FROM wp_postmeta B where B.meta_key = 'attribute_pa_renk'
        and ((B.meta_value = 'bordo') OR ('bordo' = 'hepsi'))
        and B.post_id in
        (
        SELECT A.post_id from wp_postmeta A
        JOIN wp_posts ON A.post_id = wp_posts.id
        JOIN wp_term_relationships
        ON wp_posts.post_parent = wp_term_relationships.object_id
        and term_taxonomy_id in ('2643','2304')
        WHERE A.meta_key = 'attribute_pa_beden'
        and A.meta_value in
        ('12yas','34yas','56yas','78yas','910yas','1112yas')

        )
        )


        And also, in wp_postmeta, replace



         KEY `post_id` (`post_id`),


        with



         KEY `post_id` (`post_id`, `meta_key`(191)),





        share|improve this answer













        While what I am proposing is not necessarily the best idea in the general case, I think for this specific situation you would be better off using nested IN clauses rather than the complicated self-join. Try this:



          UPDATE wp_postmeta set meta_value = 'outofstock' where meta_key = '_stock_status' and 
        post_id in
        (
        SELECT B.post_id FROM wp_postmeta B where B.meta_key = 'attribute_pa_renk'
        and ((B.meta_value = 'bordo') OR ('bordo' = 'hepsi'))
        and B.post_id in
        (
        SELECT A.post_id from wp_postmeta A
        JOIN wp_posts ON A.post_id = wp_posts.id
        JOIN wp_term_relationships
        ON wp_posts.post_parent = wp_term_relationships.object_id
        and term_taxonomy_id in ('2643','2304')
        WHERE A.meta_key = 'attribute_pa_beden'
        and A.meta_value in
        ('12yas','34yas','56yas','78yas','910yas','1112yas')

        )
        )


        And also, in wp_postmeta, replace



         KEY `post_id` (`post_id`),


        with



         KEY `post_id` (`post_id`, `meta_key`(191)),






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 8 hours ago









        Old ProOld Pro

        1212




        1212






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Database Administrators Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f229348%2fimprove-query-performance-of-select-statment-inside-in-statment%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            RAC Tourist Trophy