CLIRunner.php
1 year ago
DataRegenerator.php
4 weeks ago
Filterer.php
4 weeks ago
LookupDataStore.php
4 months ago
TaxQuery.php
4 weeks ago
TaxQuery.php
45 lines
| 1 | <?php declare( strict_types=1 ); |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Internal\ProductAttributesLookup; |
| 4 | |
| 5 | /** |
| 6 | * Tax query class introduced to optimize SQL performance in bigger product/category catalogs. |
| 7 | */ |
| 8 | class TaxQuery extends \WP_Tax_Query { |
| 9 | /** |
| 10 | * Generates SQL JOIN and WHERE clauses for a "first-order" query clause. |
| 11 | * |
| 12 | * @since 10.9.0 |
| 13 | * |
| 14 | * @param array $clause Query clause (passed by reference). |
| 15 | * @param array $parent_query Parent query array. |
| 16 | * @return array { |
| 17 | * Array containing JOIN and WHERE SQL clauses to append to a first-order query. |
| 18 | * |
| 19 | * @type string[] $join Array of SQL fragments to append to the main JOIN clause. |
| 20 | * @type string[] $where Array of SQL fragments to append to the main WHERE clause. |
| 21 | * } |
| 22 | */ |
| 23 | public function get_sql_for_clause( &$clause, $parent_query ) { |
| 24 | global $wpdb; |
| 25 | |
| 26 | // Optimization note: targeting only the 'IN' operator, where the default 'LEFT JOIN' causes performance issues. |
| 27 | if ( 'IN' !== $clause['operator'] ) { |
| 28 | return parent::get_sql_for_clause( $clause, $parent_query ); |
| 29 | } |
| 30 | |
| 31 | // Call the parent method so it does necessary cleanup with its private APIs. |
| 32 | $fallback = parent::get_sql_for_clause( $clause, $parent_query ); |
| 33 | if ( array( '' ) === $fallback['join'] && array( '0 = 1' ) === $fallback['where'] ) { |
| 34 | return $fallback; |
| 35 | } |
| 36 | |
| 37 | // Optimization note: 'fan-out LEFT JOIN' -> 'pre-materialized subquery' transition for better SQL performance. |
| 38 | $terms = implode( ',', array_map( 'absint', $clause['terms'] ) ); |
| 39 | return array( |
| 40 | 'join' => array( '' ), |
| 41 | 'where' => array( "$this->primary_table.$this->primary_id_column IN ( SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN ( $terms ) )" ), |
| 42 | ); |
| 43 | } |
| 44 | } |
| 45 |