by tremon · 5y ago · 0 comments
Avoid them on the column-side of expressions. This is called sargability [1], and refers to the ability of the query engine to limit the search to a specific index entry or data range. For example, WHERE SUBSTRING(field, 1, 1) = "A" will still cause a full table scan and the SUBSTRING function will be evaluated for every row, while WHERE field LIKE "A%" can use a partial index scan, provided an index on the field column exists.
Prefer = to LIKE
And therefore this advice is wrong. As long as your LIKE expression doesn't start with a wildcard, LIKE can use an index just fine.
Filter with WHERE before HAVING
This usually isn't an issue, because the search terms you would use under HAVING can't be used in the WHERE clause. But yes, the other way around is possible, so the rule of thumb is: if the condition can be evaluated in the WHERE clause, it should be.
WITH
Be aware that not all database engines perform predicate propagation across CTE boundaries. That is, a query like this:
WITH allRows AS (
SELECT id,
result = difficult_calculation(col)
FROM table)
SELECT result
FROM allRows
WHERE id = 15;
might cause the database engine to perform difficult_calculation() on all rows, not just row 15. All big databases support this nowadays, but it's not a given.And MySQL seems to support "generated columns" which can be "virtual" and can have indexes. (Although in that case the expression lives in the column definition, so it's not actually in a where clause.)
Also, I guess some databases probably let you have an index on a view, which could be another way.
So if you really need a function in your where clause, there may very well be a way to do it efficiently. Of course, the usual caveat applies that it requires more I/O to maintain more indexes.
I found this article on the topic to be helpful:
https://saveriomiroddi.github.io/An-introduction-to-function...
For example, in Apache Impala and Spark, "Prefer = to LIKE" is good advice, especially in join conditions, where an equijoin would allow the query planner to use a Hash Join, whereas a non equijoin limits the query planner to a Nested Loop join.
People treat deciding your app will have a database as a design decision when in reality it is only about 10% of a design decision.
Good blog post about it : https://about.gitlab.com/blog/2016/03/18/fast-search-using-p...
The docs: https://www.postgresql.org/docs/current/pgtrgm.html
Basically, instead of writing
"inner join table2 on"
you can just write "and" and put it after your other where clauses.
It doesn't result in errors because the query will fail if referring to fields from the second table when there is no join.
Could even put the join where clauses on a separate line to split them out from the other where clauses.
It does require reworking the query if not doing an inner join but that is what at least I usually want when doing ad hoc queries. Agree that the join syntax should be used in production code.
Usually, this doesn't make a difference as an EXISTS check is mostly used on the primary key (or business key) of a table, which is (hopefully) non-nullable. But it can sometimes give surprising results when using EXISTS on a nullable column, or for worse results, NOT IN.