MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 13th, 2012, 02:58 PM
Jaquline Jaquline is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 5 Jaquline User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old July 13th, 2012, 03:12 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,373 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 6 h 54 m 49 sec
Reputation Power: 4140
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
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old July 13th, 2012, 03:20 PM
Jaquline Jaquline is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 5 Jaquline User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 37 m 56 sec
Reputation Power: 0
Hi r937, currently there is no index in the table and I want suggestion related to that. Simple select query:


SELECT query from data_table where userid = '43211';


Thanks.

Reply With Quote
  #4  
Old July 13th, 2012, 03:36 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,373 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 6 h 54 m 49 sec
Reputation Power: 4140
for that query, you would want to have an index on userid

Reply With Quote
  #5  
Old July 13th, 2012, 04:19 PM
Jaquline Jaquline is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 5 Jaquline User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 37 m 56 sec
Reputation Power: 0
Quote:
Originally Posted by r937
for that query, you would want to have an index on userid


I also need to run query like:


Select * from datatable where query = 'time to go crazy';

OR

Select * from datatable where query like '%hot cakes%';


Since query field is too big how can I add index to it?

Thanks.

Reply With Quote
  #6  
Old July 13th, 2012, 04:29 PM
Guelphdad's Avatar
Guelphdad Guelphdad is offline
Hockey face
Dev Shed God 7th Plane (8000 - 8499 posts)
 
Join Date: Nov 2001
Location: St. Catharines, Canada
Posts: 8,141 Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 3 Weeks 3 Days 20 h 34 m 13 sec
Reputation Power: 1315
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.

Reply With Quote
  #7  
Old July 13th, 2012, 05:24 PM
Jaquline Jaquline is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 5 Jaquline User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #8  
Old July 14th, 2012, 09:31 AM
Guelphdad's Avatar
Guelphdad Guelphdad is offline
Hockey face
Dev Shed God 7th Plane (8000 - 8499 posts)
 
Join Date: Nov 2001
Location: St. Catharines, Canada
Posts: 8,141 Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level)Guelphdad User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 3 Weeks 3 Days 20 h 34 m 13 sec
Reputation Power: 1315
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.

Reply With Quote
  #9  
Old July 14th, 2012, 10:26 AM
Jaquline Jaquline is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 5 Jaquline User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #10  
Old July 17th, 2012, 05:19 PM
sr sr is offline
Problem Solver
Dev Shed Specialist (4000 - 4499 posts)
 
Join Date: Jan 2001
Location: Stockholm, Sweden
Posts: 4,430 sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 7 h 23 m 50 sec
Reputation Power: 532
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Please Help Me Optimize a Table

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap