开发者

PostgreSQL Query precedence

开发者 https://www.devze.com 2023-04-09 10:54 出处:网络
Here in the query when i am trying to execute the OR condition before AND conditions it gives wrong result..as web_filename_username_4hr_ts_201109 also gets included..

Here in the query when i am trying to execute the OR condition before AND conditions it gives wrong result..as web_filename_username_4hr_ts_201109 also gets included.. Wrong Result开发者_如何学Python Query:

SELECT tablename FROM pg_tables
 WHERE schemaname = 'public'
   and (tablename like 'web%_4hr_ts_%'
       and tablename not like 'webs%_4hr_ts_%'
       and tablename not like 'webr%_4hr_ts_%'
        or tablename like '%web%summary%_4hr_ts_%'
       and tablename not like 'web_filename_username%_4hr_ts_%'
       )
     ;

But when i executed all AND before ORing it gives Correct result... Correct Result Query:

SELECT tablename
  FROM pg_tables
 WHERE schemaname = 'public'
   and (tablename like 'web%_4hr_ts_%'
    and tablename not like 'webs%_12hr_ts_%'
    and tablename not like 'webr%_4hr_ts_%'
    and tablename not like 'web_filename_username%_4hr_ts_%'
     or tablename like '%web%summary%_4hr_ts_%'
       )

I dont know why....?????????


Because the way you have written it it checks in to blocks.

tablename like 'web%_4hr_ts_%' and tablename not like 'webs%_4hr_ts_%' and tablename not like 'webr%_4hr_ts_%'

or

tablename like '%web%summary%_4hr_ts_%' and tablename not like 'web_filename_username%_4hr_ts_%'

now obviously tablename can not match both '%web%summary%_4hr_ts_%' and 'web_filename_username%_4hr_ts_%' so thats always going to be false.

Here is what explain says about it

 Nested Loop  (cost=0.00..28.55 rows=1 width=64)
   Join Filter: (c.relnamespace = n.oid)
   ->  Seq Scan on pg_class c  (cost=0.00..27.45 rows=1 width=72)
         Filter: ((relkind = 'r'::"char") AND (((relname ~~ 'web%_4hr_ts_%'::text) AND (relname !~~ 'webs%_4hr_ts_%'::text) AND (relname !~~ 'webr%_4hr_ts_%'::text)) OR ((relname ~~ '%web%summary%_4hr_ts_%'::text) AND (relname !~~ 'web_filename_username%_4hr_ts_%'::text))))
   ->  Seq Scan on pg_namespace n  (cost=0.00..1.09 rows=1 width=4)
         Filter: (n.nspname = 'public'::name)

As you can see its added brackets showing that the OR is at the top level. This is why when Im using any OR with AND I always use brackets to ensure the correct evaluation order.

If your ever wondering why a statement doesn't give you what you expect just stick explain in front of it and see what its going to do.


Its' dependent on its order of execution, which is normally left to right.

You can over come this using brackets.

0

精彩评论

暂无评论...
验证码 换一张
取 消