Can adding an index make a non SARGable query SARGable?
sqlservercode.blogspot.com
sqlservercode.blogspot.com
Database management solution (sql server, postgres, mysql, etc) Database version (mysql 5.6, 5.7, 5.8, mariadb, etc)
This method of finding whether your hunch works or not is the easiest alternative other than trying to read the code (or documentation, which many db engines have excellent documentation for this sort of thing).
http://www.postgresql.org/docs/9.4/static/using-explain.html
(Blog post I wrote) http://jimkeener.com/posts/explain-pg
I'm plagued by cases where the same schema with the same indices on same version of servers running on the same architecture gets different plans and performance.
The value this provides is making development very agile (in the dictionary sense of the word).
It means that the database take your Search ARGuments (SARG) and do an index seek to deliver the exact results you want.
It depends heavily how smart the optimizer of the underlying database is in order to pick up on the fact that there's another column or index made that's precomputed for `Right(...)` with the correct arguments applied to it.
foo=# create table foo(x TEXT);
foo=# CREATE INDEX foo_r3 on foo (right(x,3));
foo=# INSERT INTO foo VALUES ('aaaaaaaaaaaaazzz');
foo=# INSERT INTO foo VALUES ('xxxxxxxaaa');
foo=# SELECT * FROM foo WHERE right(x,3) > 'fff';
x
------------------
aaaaaaaaaaaaazzz
(1 row)
foo=# EXPLAIN ANALYZE SELECT * FROM foo WHERE right(x,3) > 'fff';
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------
Bitmap Heap Scan on foo (cost=7.66..24.46 rows=453 width=32) (actual time=0.018..0.018 rows=1 loops=1)
Recheck Cond: ("right"(x, 3) > 'fff'::text)
Heap Blocks: exact=1
-> Bitmap Index Scan on foo_r3 (cost=0.00..7.55 rows=453 width=0) (actual time=0.010..0.010 rows=1 loops=1)
Index Cond: ("right"(x, 3) > 'fff'::text)
Planning Time: 0.072 ms
Execution Time: 0.063 ms
(7 rows)Why not just use a function index?
Close - it refers to SQL Server being able to take your Search ARGuments (SARG) and do an index seek to jump to the rows you're looking for.
> Is this a sql server specific term?
No: https://dba.stackexchange.com/questions/162263/what-does-the...
> Why not just use a function index?
Microsoft SQL Server doesn't have those.
It’s things like this that keep us paying $millions for SQL Server Enterpise vs. Postgres.
https://docs.microsoft.com/en-us/sql/relational-databases/vi...
See https://www.postgresql.org/docs/current/sql-createindex.html
> An index field can be an expression computed from the values of one or more columns of the table row. This feature can be used to obtain fast access to data based on some transformation of the basic data. For example, an index computed on upper(col) would allow the clause WHERE upper(col) = 'JIM' to use an index.
No need to maintain a separate view or its index
There's always a self-join or a left join or a sqlclr function or a format or an unsupported aggregate or something else that was a critical thing that I really needed in the index.
If the Microsoft SQL Server team really thinks this feature is a critical selling point, maybe they should work more on it since they were introduced back in like SQL Server 2005 and still have the same miserable whips-and-chains-bondage level masochism experience.
Using these millions to better design your applications is better in the long run. You can totally live without those with a good schema and better queries.
I think features like column store and always on are worth more that those band-aid features aimed at pleasing ugly ERPs.