?
Solved

sql

Posted on 2011-09-24
9
Medium Priority
?
243 Views
Last Modified: 2012-05-12
how can I rewrite / optimize the following query to execute faster:

SELECT count( 1 ) AS ct
FROM myTable
WHERE email =' test@test.com'
AND substr(submitdate, 1, 10 ) = substr( now( ) , 1, 10 )

I basically want it to search through today's matching records only rather than all matching records. I already created a combined index (ALTER TABLE `myTable`
ADD INDEX `newindex` (`submitdate`, `email`) ) but not sure what to do after that.  When I do an EXPLAIN, it seems to be searching all rows with matching email (regardless of date) - rather than just rows with submitdate of today. ??

thanks in advance for your help...
0
Comment
Question by:web5dev7
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 52

Expert Comment

by:_agx_
ID: 36593554
When possible, avoid using functions on columns because it can prevent the db from using your index. But you don't need substr() to find records for a single date. Just use

SELECT count(*) AS ct
FROM    myTable
WHERE  email =' test@test.com'
WHERE  submitdate >= curDate()                
AND       submitdate <  DATE_ADD(curDate(), INTERVAL 1 DAY)  

0
 
LVL 52

Expert Comment

by:_agx_
ID: 36593585
> WHERE  submitdate >= curDate()                
> AND       submitdate <  DATE_ADD(curDate(), INTERVAL 1 DAY)  

ie
WHERE  submitdate >= '2011-09-24'      --- today at midnight
AND       submitdate <  '2011-09-25'       --- tomorrow at midnight

... which essentially says find all records with today's date
0
 

Author Comment

by:web5dev7
ID: 36593932
ok, but just to clarify, this is part of a php form validation to prevent a user from submitting a form more than once per day - so I wanted it to check for the existence of his email address in all pre-existing records with a submitdate of today.  But, for efficiency sake, not have it look at all records, only today's. The dates in the db table are formatted like: 2011-09-24 15:35:15   so we don't want the time to mess it up, right ?  
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 52

Expert Comment

by:_agx_
ID: 36594104
Did you test the query? Because time isn't an issue. That's another way of saying find all records with today's date regardless of the time .  ie any time between 12 midnight and 11:59:59PM

      WHERE  submitdate >= '2011-09-24'      --- today at midnight
      AND       submitdate <  '2011-09-25'       --- tomorrow at midnight



0
 

Author Comment

by:web5dev7
ID: 36596033
phpMyAdmin gives me back the following error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near:
WHERE  submitdate >= curDate()                
AND       submitdate <  DATE_ADD(curD'
at line 4
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 1000 total points
ID: 36596803
My bad.  Copy paste error in the sql. Try this:

SELECT count(*) AS ct
FROM    myTable
WHERE  email =' test@test.com'
AND       submitdate >= curDate()                
AND       submitdate <  DATE_ADD(curDate(), INTERVAL 1 DAY)  

0
 

Author Comment

by:web5dev7
ID: 36597100
great that works in phpMyAdmin.  How could I write that in a secure way in my php select statement?  This is what phpMyAdmin gave me:

"SELECT count(*) AS ct\n"
    "FROM myTable\n"
    "WHERE email =\'$email\'\n"
    "AND submitdate >= curDate() \n"
    "AND submitdate < DATE_ADD(curDate(), INTERVAL 1 DAY)";

Do I have to do something with "mysql_real_escape_string" for security ?
Do I need the \n ? (what are those?)

0
 
LVL 52

Expert Comment

by:_agx_
ID: 36597246
Sorry, I'm not a php expert :(  As I think we've answered the original question, it's probably best to open a new thread about the sql injection question.  

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_686-PHP-Prevent-SQL-Injection.html
0
 

Author Comment

by:web5dev7
ID: 36597396
ok, will do, thanks for your help.
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question