0

In postgresql

select * from test where text123 = '1'

"Index Scan using ix_test on test (cost=0.57..12619.44 rows=6980 width=343)"
"  Index Cond: ((text123)::text = '1'::text)"



select * from test where text123 = ''

"Seq Scan on test (cost=0.00..11918891.20 rows=209355618 width=343)"
"  Filter: ((text123)::text = ''::text)"

first query result returns immediately. but second isn't.

In Oracle, it has same plan with postgresql but second query result returns immediately.

What can I do for second query in postgresql? And why second query is so slow?

please help me...

4
  • Try analyze test; in postgresql. It should just recompute the stats on the test table so the query plan can be the best available. Change the select list when you test the query again, eg select text123 from test where text123 = '1' so you can be sure it's re-preparing the query plan. Report back with the result - this may or may not fix it immediately. Commented Jul 12, 2016 at 1:57
  • First, thanks about your advise. I tried your suggestion. but, it is not working. Can you give another way for me? Commented Jul 12, 2016 at 2:33
  • In Oracle '' is the same as NULL. In PostgreSQL they are not the same. No idea why comparing to '' (empty string) should take longer than comparing to '1' - perhaps there are very few rows with '1' but many with ''? Try where text123 IS NULL in PostgreSQL and see what happens. Commented Jul 12, 2016 at 2:34
  • It isn't full query. It is subquery for another main query. This subquery return result ''. so I tested like that. Commented Jul 12, 2016 at 2:36

1 Answer 1

2

The second query

select * from test where text123 = ''

does very different things in PostgreSQL and Oracle.

In PostgreSQL '' means "a string of zero length". Thus in PostgreSQL the query means

Return all fields in all rows of table test where the text123 column
is equal to a string which is zero characters long

In Oracle, however '' means NULL. Thus in Oracle this query means

Return all fields in all rows of table TEST where the TEXT123 column
is equal to NULL

and because nothing, not even NULL, is ever equal to NULL the query will return nothing.

Best of luck.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I got useful knowledge from you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.