Thanks
Main Topics
Browse All Topicsi have a table called road
It has
select count(*) from road;
count
---------
1098822
I want to fire this query in the database
select name, astext(the_geom) from road where lower(city) like '%mumbai%' and lower(name) like '%carter%';
explain analyze select name, astext(the_geom) from road where lower(city) like '%mumbai%' and lower(name) like '%carter%';
QUERY PLAN
--------------------------
Seq Scan on road (cost=0.00..45164.62 rows=1 width=478) (actual time=282.400..1299.810 rows=16 loops=1)
Filter: ((lower((city)::text) ~~ '%mumbai%'::text) AND (lower((name)::text) ~~ '%carter%'::text))
Total runtime: 1299.864 ms
(3 rows)
I would like suggestions from experts as to a way to reduce the time for execution or may be optimize the same for better performance
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: grant300Posted on 2008-08-14 at 10:35:23ID: 22232358
Your WHERE clauses pretty well assure a complete table scan. There are two reasons.
First, you are using a function, lower(), around the columns. When you do that, every city and name value must be pulled out of the table and run through the function in order to allow for comparison.
Second, you are using leading wildcard LIKE comparisons. This also requires that every value from the city and name fields be retrieved and run through the pattern match.
If I am reading the Query Plan correctly, you are getting a runtime of 1.3 seconds. Quite frankly, given the brute-force path this query has to take, I am pretty impressed it runs that fast. What is the actual runtimes you are seeing?
You really can't speed this up without making some design changes. For instance, you could stuff only lower case into the city and name fields to begin with. That way you could use the lower() function on the search string which, of course, only happens once, not 1 million times per column.
The other thing you need to figure out how to do is remove the leading wildcards. Without leading wildcards, you can place indexes on city and/or name and the query processor can then use them. You may have problems with all kind of garbage in the city and name fields, however, if you can quantify a technique for pulling out just the major item, e.g. "N. Carter Rd." becomes "carter", you can build additional computed column(s) which could potentially be much more useful, particularly if indexed and used in an exact match.
Regards,
Bill