jaymc Posted August 11, 2008 Share Posted August 11, 2008 I have a table with 1.7million rows in it I am running this query which is taking 100+ seconds SELECT * FROM postcodes WHERE replace(postcode,' ','') = 'l12 8uj' OR postcode = 'l12 8uj' LIMIT 0,1 postcode has an index, I think the issue is the REPLACE, perhaps it has to analyze each row to do the REPLACE where as it would normally skip through it Any ideas on optimizing the table/query to help with this request Thanks Quote Link to comment Share on other sites More sharing options...
friedemann_bach Posted August 11, 2008 Share Posted August 11, 2008 Submitting your table structure (fields, data types and indexes) would help. Quote Link to comment Share on other sites More sharing options...
jaymc Posted August 11, 2008 Author Share Posted August 11, 2008 Field Type Collation Attributes Null Default Extra Action postcode varchar(10) latin1_general_ci No Browse distinct values Change Drop Primary Unique Index Fulltext street varchar(255) latin1_general_ci Yes NULL Browse distinct values Change Drop Primary Unique Index Fulltext city varchar(255) latin1_general_ci Yes NULL Browse distinct values Change Drop Primary Unique Index Fulltext county varchar(255) latin1_general_ci Yes NULL Browse distinct values Change Drop Primary Unique Index Fulltext street2 varchar(255) latin1_general_ci Yes NULL Browse distinct values Change Drop Primary Unique Index Fulltext town varchar(50) latin1_swedish_ci No Browse distinct values Change Drop Primary Unique Index Fulltext x varchar(50) latin1_swedish_ci No Browse distinct values Change Drop Primary Unique Index Fulltext y varchar(50) latin1_swedish_ci No Browse distinct values Change Drop Primary Unique Index Fulltext longitude varchar(50) latin1_swedish_ci No Browse distinct values Change Drop Primary Unique Index Fulltext latitude varchar(50) latin1_swedish_ci No Quote Link to comment Share on other sites More sharing options...
jaymc Posted August 11, 2008 Author Share Posted August 11, 2008 Indexes: Documentation Keyname Type Cardinality Action Field postcode INDEX 1794204 Edit Drop postcode Quote Link to comment Share on other sites More sharing options...
friedemann_bach Posted August 11, 2008 Share Posted August 11, 2008 Questions: - Which field is your primary key? Do you have a PK at all? If not, add a field like 'id', make it an integer(9), set it to auto_increment and make it your PRIMARY KEY. - Your query (replace(postcode,' ','') = 'l12 8uj') : what sense does it make to replace ' ' with '' and then search for a string that contains ' ' ? This won't return any result at all. Please explain your idea. General advice: - Disable the NULL values for the fields 'street', 'city', 'county' and 'street2'. Probably you don't require to distinct between NULL and an empty string. - Fields containing arithmetic information (x,y,longitude,latitude) do not need to be VARCHAR, make them INTEGER if possible or convert the values to integers if possible. Quote Link to comment Share on other sites More sharing options...
jaymc Posted August 11, 2008 Author Share Posted August 11, 2008 Questions: - Which field is your primary key? Do you have a PK at all? If not, add a field like 'id', make it an integer(9), set it to auto_increment and make it your PRIMARY KEY. I DONT HAVE A PRIMARY KEY, WHY DO I NEED ONE? AN INDEX ON POSTCODE IS ENOUGH SURELY? OR DOES A PRIMARY KEY INDEX BETTER - Your query (replace(postcode,' ','') = 'l12 8uj') : what sense does it make to replace ' ' with '' and then search for a string that contains ' ' ? This won't return any result at all. Please explain your idea. BECAUSE PEOPLE ENTER A POSTCODE LIKE "l12 8uj" OR "l128uj". THE QUERY WILL MATCH EITHER Quote Link to comment Share on other sites More sharing options...
unkwntech Posted August 11, 2008 Share Posted August 11, 2008 My suggestion here ( not an SQL genius) I would just format the data how YOU want it before you insert it. Quote Link to comment Share on other sites More sharing options...
jaymc Posted August 11, 2008 Author Share Posted August 11, 2008 Yeh, I think I'll have to as it looks like the REPLACE screws the effeciency INDEX up Quote Link to comment Share on other sites More sharing options...
friedemann_bach Posted August 13, 2008 Share Posted August 13, 2008 BECAUSE PEOPLE ENTER A POSTCODE LIKE "l12 8uj" OR "l128uj". THE QUERY WILL MATCH EITHER Nope. Quite wrong. replace(postcode,' ','') = 'l12 8uj' does never match anything: the postcode 'l12 8uj' contains ' ' which would be replaced by replace(postcode,' ',''). 'l128uj' will probably never match 'l12 8uj'. I think it should read replace(postcode,' ','') = 'l128uj', without ' '. Then it might work as you thought. As proposed by unkwntech, try to optimize your data entries but also try to optimize the database structure as I proposed above. I did a test on a large database (~200.000 rows) and even queries containing a replace statement took less than 1 sec. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.