SELECT tt.trans_type_name AS transaction_type,
trans.transaction_time,
a.trans_action_name AS transaction_action_name,
trans.transaction_notes
FROM(SELECT Cast(added_on AS CHAR) AS transaction_time,
notes AS transaction_notes,
trans_type_id AS trans_type,
trans_action_id AS action_id
FROM transactions
WHERE added_by = 'service_app'
AND notes LIKE %(domain)s
AND added_on BETWEEN %(transactionstartdate)s
AND %(transactionenddate)s) trans
LEFT JOIN transaction_types tt
ON trans.trans_type = tt.trans_type_id
LEFT JOIN transaction_actions a
ON trans.action_id = a.trans_action_id
ORDER BY trans.transaction_time DESC
This is a query I rewritten because the older query had the error:
The SELECT would examine more than MAX_JOIN_SIZE rows; check your WHERE and use SET SQL_BIG_SELECTS=1 or SET MAX_JOIN_SIZE=# if the SELECT is okay
The new query(written above) is more faster but I am still getting the MAX_JOIN error. The thing is, I cannot set SET SQL_BIG_SELECTS=1. My wish is evaluating the subquery first because for the values I plug, there are only 3 rows. The join needs to only happen for the 3 rows since it is just replacing the ID with it's corresponding value.
Is there a way? Thanks in advance.
Edit: Some more clarity - 3 tables transactions, transaction_types and transaction_actions. The base table is the transactions which has more than million rows. It has two IDs which references the other two tables. I want to select the names instead of IDs so joining it.
SET SESSION SQL_BIG_SELECTS=1;andSET SESSION MAX_JOIN_SIZE = 18446744073709551615;does not need in any special privilege. How to make MySQL evaluate subquery first Try to define it in CTE. As an extreme option addLIMIT 0, 18446744073709551615to it. And, ifadded_oncolumn datatype is DATETIME/TIMESTAMP then you do not need in explicit CAST.(added_by, added_on)or(added_on, added_by)depending on selectivty. With up-to-date statistics, the DBMS should detect that it pays to get the transactions rows first.added_on? From its name, it sounds like a date, but you cast it into a string and then call it time, however, you useBETWEEN, so I'd guess on a date again. Or is that even some number or string? (2) Then, if you are interested in the domain, why is it part of a notes string and not a separate column? Storing the domain separately from unformatted notes might speed up such queries a lot.max_join_sizeon your MySQL server? The default is very large (max BIGINT value), so the admin must have reduced it deliberately. Perhaps they reduced it too much. You should talk to them about your problem.