WP_Meta_Query::find_compatible_table_alias( array $clause, array $parent_query ): string|false
- Since
- 4.1.0
- Source
wp-includes/class-wp-meta-query.php:830
Description
We avoid unnecessary table joins by allowing each clause to look for an existing table alias that is compatible with the query that it needs to perform.
An existing alias is compatible if (a) it is a sibling of $clause (ie, it's under the scope of the same relation), and (b) the combination of operator and relation between the clauses allows for a shared table join.
In the case of WP_Meta_Query, this only applies to 'IN' clauses that are connected by the relation 'OR'.
Compatibility
- WordPress
- since 4.1.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$clausearray- Query clause.
$parent_queryarray- Parent query of $clause.
Return value
string|false- Table alias if found, otherwise false.
Performance profile
How much work a call to WP_Meta_Query::find_compatible_table_alias() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Light
- Scaling
- Scales with input
- Instructions
- 14–55
- Plugin surface
- 1 hook
- Called by
- 1
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 57.
Third-party callbacks on 'meta_query_find_compatible_table_alias' run inside this call, and their cost is not bounded by anything here.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly
What one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Meta_Query::find_compatible_table_alias() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 14–15 | apply_filters() |
!$parent_query && !empty($sibling) && is_array($sibling) && ->is_first_order_clause() && $clause_compare && $sibling_compare | 48–55 | ->is_first_order_clause(), strtoupper(), strtoupper(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 57 | 14–55 | 11 | |
| 8.5 | 57 | 14–55 | 11 | |
| 8.4 | 57 | 14–55 | 11 | 10 fewer instructions than PHP 8.3 |
| 8.3 | 67 | 14–65 | 11 | |
| 8.2 | 67 | 14–65 | 11 | |
| 8.1 | 67 | 14–65 | 11 | |
| 7.4 | 67 | 14–65 | 11 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while WP_Meta_Query::find_compatible_table_alias() runs, in this order:
- apply_filters( meta_query_find_compatible_table_alias )filterline 873 (+43 into the body)
Filters the table alias identified as compatible with the current clause.
Uses · 2
- apply_filters()Calls the callback functions that have been added to a filter hook.
- WP_Meta_Query::is_first_order_clause()Determines whether a query clause is first-order.
Used by · 1
- WP_Meta_Query::get_sql_for_clause()Generates SQL JOIN and WHERE clauses for a first-order query clause.
Source code
protected function find_compatible_table_alias( $clause, $parent_query ) { $alias = false; foreach ( $parent_query as $sibling ) { // If the sibling has no alias yet, there's nothing to check. if ( empty( $sibling['alias'] ) ) { continue; } // We're only interested in siblings that are first-order clauses. if ( ! is_array( $sibling ) || ! $this->is_first_order_clause( $sibling ) ) { continue; } $compatible_compares = array(); // Clauses connected by OR can share joins as long as they have "positive" operators. if ( 'OR' === $parent_query['relation'] ) { $compatible_compares = array( '=', 'IN', 'BETWEEN', 'LIKE', 'REGEXP', 'RLIKE', '>', '>=', '<', '<=' ); // Clauses joined by AND with "negative" operators share a join only if they also share a key. } elseif ( isset( $sibling['key'] ) && isset( $clause['key'] ) && $sibling['key'] === $clause['key'] ) { $compatible_compares = array( '!=', 'NOT IN', 'NOT LIKE' ); } $clause_compare = strtoupper( $clause['compare'] ); $sibling_compare = strtoupper( $sibling['compare'] ); if ( in_array( $clause_compare, $compatible_compares, true ) && in_array( $sibling_compare, $compatible_compares, true ) ) { $alias = preg_replace( '/\W/', '_', $sibling['alias'] ); break; } } /** * Filters the table alias identified as compatible with the current clause. * * @since 4.1.0 * * @param string|false $alias Table alias, or false if none was found. * @param array $clause First-order query clause. * @param array $parent_query Parent of $clause. * @param WP_Meta_Query $query WP_Meta_Query object. */ return apply_filters( 'meta_query_find_compatible_table_alias', $alias, $clause, $parent_query, $this ); }Changelog
Introduced in 4.1.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 tag, from
src/wp-includes/class-wp-meta-query.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.