The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Databases
> MySQL Help
|
Please Help Me Optimize a Table
Discuss Please Help Me Optimize a Table in the MySQL Help forum on Dev Shed. Please Help Me Optimize a Table MySQL Help forum discussing administration, SQL syntax, and other MySQL-related topics. MySQL is an open-source relational database management system (RDBMS).
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

July 13th, 2012, 02:58 PM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 5
Time spent in forums: 37 m 56 sec
Reputation Power: 0
|
|
|
Please Help Me Optimize a Table
Hi,
I've MySQL Db with single table with following fields:
userid: int (data is in format: 447520)
query: varchar (300) (general web queries)
datetime varchar (20) (format: 5/18/2006 9:21)
position: int (format: 425)
clickurl: varchar (100) (format: web urls)
There are 37.5 million rows in the table and it is tanking 3-5 minutes to execute simple select queries. Please let me know what changes should I do to the table and/or DB to make it execute queries as fast as possible.
Also please let me know how should the data in the given format be inserted into table so as to make sense or considered as date/time.
Thanks.
|

July 13th, 2012, 03:12 PM
|
 |
SQL Consultant
|
|
Join Date: Feb 2003
Location: Toronto Canada
|
|
Quote: | Originally Posted by Jaquline ... it is tanking 3-5 minutes to execute simple select queries. | please do a SHOW CREATE TABLE so that we can see the indexes
please show example of query along with the EXPLAIN output
|

July 13th, 2012, 03:20 PM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 5
Time spent in forums: 37 m 56 sec
Reputation Power: 0
|
|
|

July 13th, 2012, 03:36 PM
|
 |
SQL Consultant
|
|
Join Date: Feb 2003
Location: Toronto Canada
|
|
|
for that query, you would want to have an index on userid
|

July 13th, 2012, 04:19 PM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 5
Time spent in forums: 37 m 56 sec
Reputation Power: 0
|
|
|

July 13th, 2012, 04:29 PM
|
 |
Hockey face
|
|
Join Date: Nov 2001
Location: St. Catharines, Canada
|
|
|
300 characters isn't too big to add an index to the column.
Any searches with % at the beginning of a search term won't use an index.
change your varchar type for your datetime column to an actual datetime type, note you are best to create a new column, reformat the data into the new column. Without doing that you can't run any date or time functions on that column. that will slow you down.
|

July 13th, 2012, 05:24 PM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 5
Time spent in forums: 37 m 56 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by Guelphdad 300 characters isn't too big to add an index to the column.
Any searches with % at the beginning of a search term won't use an index.
change your varchar type for your datetime column to an actual datetime type, note you are best to create a new column, reformat the data into the new column. Without doing that you can't run any date or time functions on that column. that will slow you down. |
Hi Guelphdad, can you please help me with the code to create a table with all that, including conversion to date/time, thanks for your reply.
|

July 14th, 2012, 09:31 AM
|
 |
Hockey face
|
|
Join Date: Nov 2001
Location: St. Catharines, Canada
|
|
First run an ALTER TABLE statement to change the table, adding the new column:
Code:
ALTER TABLE
yourtablenamegoeshere
ADD
newdatetimecolumn DATETIME
AFTER
`datetime` -- backticks needed if your column is called datetime
now run an update
Code:
UPDATE
yourtablename
SET
newdatetimecolumn = STR_TO_DATE(`datetime`, '%m/%d/%Y %h:%i')
verify by looking at your data to see that worked correctly, if so drop the old column:
Code:
ALTER TABLE
yourtablename
DROP
`datetime`
Last edited by Guelphdad : July 14th, 2012 at 09:36 AM.
|

July 14th, 2012, 10:26 AM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 5
Time spent in forums: 37 m 56 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by Guelphdad First run an ALTER TABLE statement to change the table, adding the new column:
Code:
ALTER TABLE
yourtablenamegoeshere
ADD
newdatetimecolumn DATETIME
AFTER
`datetime` -- backticks needed if your column is called datetime
now run an update
Code:
UPDATE
yourtablename
SET
newdatetimecolumn = STR_TO_DATE(`datetime`, '%m/%d/%Y %h:%i')
verify by looking at your data to see that worked correctly, if so drop the old column:
Code:
ALTER TABLE
yourtablename
DROP
`datetime`
|
Guelphdad I will try your code and let you know of the outcome, thanks.
|

July 17th, 2012, 05:19 PM
|
|
Problem Solver
|
|
Join Date: Jan 2001
Location: Stockholm, Sweden
|
|
Quote: | Originally Posted by Guelphdad
Any searches with % at the beginning of a search term won't use an index.
|
Just to clarify, it will use an index but it won't use it in a good way since it needs to scan and compare all records.
But then again, scanning an index that only contains the data of one column is usually much better than scanning the entire table that contains the data of all columns.
__________________
/Stefan
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|