I've stumbled upon the optimizer hints
MySQL feature here that I did not know existed and tried to figure out how to use this with Yii2
.
MySQL supports setting specific options by using optimizer hints
, e.g.:
SELECT /*+ NO_RANGE_OPTIMIZATION(t3 PRIMARY, f2_idx) */ f1
FROM t3 WHERE f1 > 30 AND f1 < 33;
SELECT /*+ BKA(t1) NO_BKA(t2) */ * FROM t1 INNER JOIN t2 WHERE ...;
SELECT /*+ NO_ICP(t1, t2) */ * FROM t1 INNER JOIN t2 WHERE ...;
SELECT /*+ SEMIJOIN(FIRSTMATCH, LOOSESCAN) */ * FROM t1 ...;
EXPLAIN SELECT /*+ NO_ICP(t1) */ * FROM t1 WHERE ...;
SELECT /*+ MERGE(dt) */ * FROM (SELECT * FROM t1) AS dt;
INSERT /*+ SET_VAR(foreign_key_checks=OFF) */ INTO t2 VALUES(2);
Is there Yii2
native way of setting those query hints
?
You can use second argument of select()
to pass select options:
$query = (new Query())
->select('f1', '/*+ NO_RANGE_OPTIMIZATION(t3 PRIMARY, f2_idx) */')
->from('t3');