Jump to content

Help with MySQL query - dates / max, etc


Roq

Recommended Posts

Basically I have a database that looks something like this:

 

 

 

                id hostname         pingmin pingavg pingmax pingdev pingloss lastupdated

145446 ts3.bo4 83.83 84.21 84.69 0.21         0.0 2010-01-18 13:48:15

145445 ts4.bo4 83.74 84.11 84.66 0.18 0.0 2010-01-18 13:48:08

145444 ts2.bo3 85.71 85.96 86.23 0.12         0.0 2010-01-18 13:48:01

145443 ts2.se1 78.89 79.13 80.66 0.37         5.0 2010-01-18 13:47:54

145442 ts1.bo4 83.82 84.12 84.68 0.19         0.0 2010-01-18 13:47:46

 

This database is generated by a pingscript that pings a list of devices (roughly 70 devices) 20 times each, every 10 minutes, then adds an entry into the database with the above information.  I store multiple rows for each hostname so that I can use the data to draw graphs with.  However for a basic status page I just want to find the latest entry for each device and display the data.  What is the best way to do this?  I can't seem to get MAX to work with a date field, or maybe I can't get the WHERE clause right, either way I'm stuck.

 

Can someone please help me with a query?

 

database name = switchbase

tablename = corp_pingdb

column names = headers from the above table, those are accurate

 

Thanks!

 

 

Link to comment
Share on other sites

I believe you mean something like this, which is what I've been trying but doesn't seem to work.  The results it spits out are dated Jan 4th (when I started collecting data).

 

SELECT MAX(`id`), `hostname`, `pingmin`, `pingavg`, `pingmax`, `pingdev`, `pingloss`, `lastupdated`

FROM `switchbase`.`corp_pingdb`

GROUP BY `hostname`

Link to comment
Share on other sites

Update:  This query seems to work correctly, but takes an unacceptable amount of time to execute, maybe 5-10s for the page to load.  Any suggestions for a way to optimize this, or perhaps a different way to do the same thing?

 

SELECT `corp_pingdb`.`id`, `corp_pingdb`.`hostname`, `corp_pingdb`.`pingmin`, `corp_pingdb`.`pingavg`, `corp_pingdb`.`pingmax`, `corp_pingdb`.`pingdev`, `corp_pingdb`.`pingloss`, `corp_pingdb`.`lastupdated` FROM corp_pingdb,

 

(SELECT hostname, MAX(`lastupdated`) as max_lastupdated FROM `switchbase`.`corp_pingdb`

GROUP BY `hostname` ORDER BY hostname asc) b

 

WHERE (`corp_pingdb`.`hostname` = b.hostname AND `corp_pingdb`.`lastupdated` = b.max_lastupdated)

  ORDER BY `corp_pingdb`.`hostname`

Link to comment
Share on other sites

id select_type table type possible_keys key key_len ref rows Extra

1 PRIMARY <derived2> ALL NULL NULL NULL NULL 73 Using temporary; Using filesort

1 PRIMARY corp_pingdb ALL NULL NULL NULL NULL 166695 Using where

2 DERIVED corp_pingdb ALL NULL NULL NULL NULL 166695 Using temporary; Using filesort

 

That what you're looking for?

Link to comment
Share on other sites

Because I'm a noobie :) 

 

What would you suggest?

 

Tangent:

I'm a network engineer that dabbles in coding.  I know enough to automate some tasks and recently decided to take a bunch of my perl / php scripts and just make a simple website to display the results or use some forms instead of cli.  This is really the first time that I've ran into performance issues since most of my scripts / tables are very simple and small.  Example:  Inventory list generated by SNMP queries of serial numbers, model number, hostname, snmp location, etc.  Maybe 300 rows or something.  Most of my tables are between 50 - 4000 rows.  This is the only table I have that is ever-growing which means now is when I have a need to learn about optimizations :)  I'm 100% selftaught based on needs, and now that I have one I need to learn, hah.

Link to comment
Share on other sites

I put an index on hostname and lastupdated and that improved the query a ton (explain shows only one 'all' query, and the otehr one is a single).  Didn't think it would be that easy hah.  Thanks for your help I'm going to mark this one as solved, for now :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.