Quote (nuvo @ Mar 15 2015 11:45am)
It is the best way, converting to varchar to take only 4 first chars and converting back to int has big performance penalty.
Something like
select .. where sixdigitnumber::varchar like '2985%';
will also work but is much uglier. Look:
Code
=> explain analyze select x from generate_series(100000, 999999) x where x/100 = 2985;
QUERY PLAN
--------------------------------------------------------------------------------
Function Scan on generate_series x (cost=0.00..15.00 rows=5 width=4) (actual t
Filter: ((x / 100) = 2985)
Total runtime: 216.342 ms
(3 rows)
vs
Code
=> explain analyze select x from generate_series(100000, 999999) x where x::varchar like '2985%';
QUERY PLAN
--------------------------------------------------------------------------------
Function Scan on generate_series x (cost=0.00..17.50 rows=5 width=4) (actual t
Filter: (((x)::character varying)::text ~~ '2985%'::text)
Total runtime: 274.146 ms
(3 rows)
awesome...incredibly helpful post. can't wait to give it a try in a bit.
One thing I forgot to mention though is that the reason I'm using the first 4 digits, is that this is for an industry code. Industry codes get more specific with every digit. I'm limiting it to 4 because I'd be excluding too much data otherwise. To give you an idea 4 digits might be restaurants (the data set I want) where six digits might be restaurants that specialize in sushi or a specific type of cuisine.
where sixdigitnumber::varchar like '2985%' may actually be more applicable. Can I leave it at that or will I need to convert it back to an integer afterwards? This is all rather new to me, and i don't want to ruin the original data set within the table.
This post was edited by noflexzone on Mar 15 2015 09:56am