MySqlTalk.com  

Go Back   MySqlTalk.com > MySQL > General
User Name
Password
FAQ Members List Calendar Search Today's Posts Mark Forums Read


Reply
 
Thread Tools Search this Thread
Old 10-13-2005, 09:21 AM   #1
hessodreamy
Junior Member
 
Join Date: May 2005
Posts: 9 hessodreamy is on a distinguished road
Conditional actions

I need to keep a running count of actions for particular dates, so I need to say 'if this date & value aren't in the table, insert them. if they are, increment them'. Is this possible to do in 1 query? I can't figure how.

Here's what I'm doing at the minute:
Code:
SELECT count(*) FROM tracking WHERE date = current_date() AND source = 'somesource' AND target = 'sometarget'
if the result is 0, then insert it:
Code:
INSERT INTO tracking (`source`,`target`, `hits`, `date`) VALUES ('somesource', 'sometarget', 1, current_date())
otherwise increment:
Code:
UPDATE tracking set hits = hits + 1 WHERE `source` = 'somesource' AND `target`='sometarget' AND date = current_date()

This seems pretty inefficient to me. Can it be done in one query? btw i'm using version 4.0
hessodreamy is offline   Fork this post Reply With Quote
Old 10-13-2005, 01:21 PM   #2
Azkaban
Senior Member
 
Join Date: Jul 2004
Posts: 167 Azkaban is on a distinguished road
Re: Conditional actions

Quote:
Originally Posted by hessodreamy
I need to keep a running count of actions for particular dates, so I need to say 'if this date & value aren't in the table, insert them. if they are, increment them'. Is this possible to do in 1 query? I can't figure how.

Here's what I'm doing at the minute:
Code:
SELECT count(*) FROM tracking WHERE date = current_date() AND source = 'somesource' AND target = 'sometarget'
if the result is 0, then insert it:
Code:
INSERT INTO tracking (`source`,`target`, `hits`, `date`) VALUES ('somesource', 'sometarget', 1, current_date())
otherwise increment:
Code:
UPDATE tracking set hits = hits + 1 WHERE `source` = 'somesource' AND `target`='sometarget' AND date = current_date()

This seems pretty inefficient to me. Can it be done in one query? btw i'm using version 4.0

Look very carefully into the REPLACE INTO syntax. It may solve your problem. But you have to really understand it well. It works based on the index. Do you know how to do multiple columns per index? REPLACE INTO will overwrite an existing line if it exists or INSERT a new one if it doesn't exist. But it is based on the unique index.

I'm not even 100% sure it would work. Hm. The more I think about it the less I think it will work. I'm not sure you can really save any steps.

Where is the bottleneck? Is it just that you want to save a step or two? Or are you doing some heavy duty logging where you call those three lines a huge number of times? Because if it's the latter there are other techniques you can do to achieve improved performance.

But to give you that I'd need a little more detail on what you're doing and what your goal is. How critical it is for each hit to be counted. Because if losing a few hits isn't critical to you I may suggest you use a table in memory. But if losing a bit of data when the mysql server goes down periodically then I'll stuck w/ a regular table.
__________________
** Most misspellings intentional to combat spaham filterz**
Azkaban is offline   Fork this post Reply With Quote
Old 10-14-2005, 04:00 AM   #3
hessodreamy
Junior Member
 
Join Date: May 2005
Posts: 9 hessodreamy is on a distinguished road
Re: Conditional actions

I've figured out the the answer is to use ON DUPLICATE KEY UPDATE.
I made a unique key out of concatenating other fields, then used the following:

Code:
INSERT INTO tracking VALUES( concat( substring('somesource',1,15),'_', substring('sometarget',1,15),'_', substring(current_date(),1,10) ), 'somesource','sometarget',1, current_date()) ON DUPLICATE KEY UPDATE hits = hits + 1

Works like a charm.
hessodreamy is offline   Fork this post Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a conditional query gcornish SQL syntax 8 08-16-2004 12:24 AM



All times are GMT -4. The time now is 08:03 PM.



Powered by: vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Limited.
Google
  Web http://www.mysqltalk.com
DISCLAIMERS:
1. We have no commercial interest in this site.
Banner Ads and Subscriptions will only be used to help pay for hosting and maintenance costs.
2. MySQLTalk.com is NOT affiliated with MySQL AB in any way.
3. MySQLTalk.com is NOT endorsed by MySQL AB in any way.
4. Please do not post any content that is harmful to MySQL or MySQL AB, meaning no misleading or obsolete information will be tolerated.
Well-founded constructive criticism meant to help the community is permitted.
5. This website is founded with the goal of improving the MySQL community.
We not only tolerate newbies, we encourage them.
Please do not ask newbies to "read the manual".