News for hackers. Stuff that matters.
Re:Can adding an index make a non SARGable query SARGable? by Shish2k on Friday October 18, 2019 @07:03PM (#21295754)
Could you not just add an index on `Right(SomeColumn,3)`? Creating indexes for common queries seems less weird than adding columns...
Re:Can adding an index make a non SARGable query SARGable? by bfung on Sunday October 20, 2019 @03:50AM (#21302758)
The function `Right(...)` is not indexable because... it's a function - we could apply infinite many types of function on column `SomeColumn` and it'd be unreasonable to create index for them all, with different arguments.

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.

Re:Can adding an index make a non SARGable query SARGable? by andrewf on Sunday October 20, 2019 @05:23AM (#21303074)
Postgres can do indexes on expressions. (A bitmap scan over 2 rows may not be the most impressive example!) https://www.postgresql.org/docs/12/indexes-expressional.html

  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)
Re:Can adding an index make a non SARGable query SARGable? by bfung on Sunday October 20, 2019 @05:54PM (#21307443)
If one thinks about how that index is created underneath the covers, it's basically precomputing that column and using it as an index! =)
Re:Can adding an index make a non SARGable query SARGable? by Tostino on Monday October 21, 2019 @09:48AM (#21312179)
Yes exactly. But it works very well in practice.
Re:Can adding an index make a non SARGable query SARGable? by hotsauceror on Friday October 18, 2019 @07:13PM (#21295799)
The issue with nonsargable expressions is not that you can’t create an index that contains what would be the output of that expression. It’s that you are forced to do a row-by-row evaluation of each record in order to even utilize the corresponding index to do a seek/scan against. You basically force a scan of the smallest available index. And there are tons of commonly used , nonsargable expressions. Implicit conversions, string parsing, date parsing, isnull/coalesce.
Re:Can adding an index make a non SARGable query SARGable? by mlyle on Friday October 18, 2019 @09:23PM (#21296252)
A lot of people are used to Postgres where you can create an index on the expression itself, and then it's cheap to seek or order on that expression. (Common ones are things like this-- substrings, date expressions, uppercasing of things..)
Re:Can adding an index make a non SARGable query SARGable? by ComputerGuru on Saturday October 19, 2019 @12:21AM (#21296738)
SQLite does this too out of the box, though many GUI tools struggle to cope with tables created in that fashion.
Re:Can adding an index make a non SARGable query SARGable? by jimktrains2 on Friday October 18, 2019 @07:41PM (#21295900)
Can you explain what sargable is? I've never used SQL server, but I'm sure a similar expression could be indexed in postgresql. (Not at a computer to check.)
Re:Can adding an index make a non SARGable query SARGable? by BrentOzar on Friday October 18, 2019 @07:55PM (#21295945)
> Can you explain what sargable is?

It means that the database take your Search ARGuments (SARG) and do an index seek to deliver the exact results you want.

Re:Can adding an index make a non SARGable query SARGable? by hotsauceror on Friday October 18, 2019 @08:18PM (#21296028)
What Brent said. I basically interpret it to mean “this expression can not be satisfied (covered) by the use of an index even if an appropriate one existed.”
Re:Can adding an index make a non SARGable query SARGable? by Pxtl on Saturday October 19, 2019 @12:51AM (#21296811)
... if isnull/coalesce is really non-sargable, it's time for me to give up and switch to Mongo.
Re:Can adding an index make a non SARGable query SARGable? by hotsauceror on Saturday October 19, 2019 @08:59AM (#21298237)
It is. You’ve applied a function to a value, and that function needs to be evaluated for every row in the set. One way to deal with cases where you need to replace Nulls, or to do any other kind of polishing, is to return the raw data into a temp table and then do an update on that temp table, replacing all nulls with your chosen value using WHERE x IS NULL. This minimizes the time you hold any locks on your live tables. And now you can index the hell out of your “staging” table and use that as your driver for additional joins etc.
Re:Can adding an index make a non SARGable query SARGable? by heavenlyblue on Sunday October 20, 2019 @06:05AM (#21303233)
Let's be honest - this is an SQL-server only issue. Nobody says you can't index NULL values, it's just that SQL server can't do that.
Re:Can adding an index make a non SARGable query SARGable? by hotsauceror on Sunday October 20, 2019 @12:02PM (#21304954)
That’s not quite true. You can certainly index null values, and you can filtered indexes to remove nulls. What sql server cannot do is index functions themselves, and use those indexes to satisfy queries with nonsargable expressions.