Solved

Optimizing two MySQL queries

Posted on 2008-06-11
5
202 Views
Last Modified: 2010-04-21
I have two simple SQL queries that I have no idea how to optimize

The first one is pretty simple:
SELECT * FROM tags WHERE tag IN (SELECT DISTINCT(tag) FROM tags WHERE datatype = 'post' AND param1 = '9525')

The table has an index on the "tag" column. It easily takes 3 seconds. Can it be optimized in any way?

The other one I think is simple (I just have limited SQL knowledge). My way of implementing it is with a loop:

SQLStr = "SELECT * FROM blogusers WHERE userid = '" & curUserID & "'"
set rstDB = cnnDB.Execute(SQLStr)
Do While Not rstDB.EOF
    tmpBlogID = rstDB.Fields("blogid").Value
    SQLStr = "UPDATE blogstats SET myself = 'yes' WHERE blogid = '" & tmpBlogID & "' AND IP = '" & ipadr & "'"
    cnnDB.Execute(SQLStr)
    rstDB.MoveNext
Loop

Can this be combined to one UPDATE SQL query?
0
Comment
Question by:logicmedia
  • 3
  • 2
5 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 21758104
>SELECT * FROM tags WHERE tag IN (SELECT DISTINCT(tag) FROM tags WHERE datatype = 'post' AND param1 = '9525')
might work faster like this:
SELECT * FROM tags t WHERE EXISTS (SELECT NULL FROM tags i WHERE i.tag = t.tag and i.datatype = 'post' AND i.param1 = '9525')

with another (1) index on datatype + param1 + tag (all 3 fields, in that order, on the index)
0
 

Author Comment

by:logicmedia
ID: 21758122
Angellll: It takes the query down to about half a second which is much better. Thanks a lot...
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21758217
ok, so now the second part:
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 500 total points
ID: 21758235
oops...

just do this:
SQLStr = "UPDATE blogstats bs SET myself = 'yes'  "
SQLStr = SQLStr & " WHERE bs.IP = '" & ipadr & "' "
SQLStr = SQLStr & " AND bs.blogid IN ( SELECT blogid FROM bloguser WHERE userid = '" & curUserID & "' ) "
 
cnnDB.Execute(SQLStr)

Open in new window

0
 

Author Closing Comment

by:logicmedia
ID: 31466071
Thanks a lot... I can't wait to get better at this ;-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

820 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