1

We have a MySQL database that we are using to measure automated test successes and failures. Now we need to query the database to find out how many failures happened per day and what the cause was. The queries I've come up with so far go something like this:

select * from failures 
where time >= '{time}' and time <= '{time} 23:59:59' and cause = '{fail_type} failed'

I have a couple of variations of this query. The problem I am running into is that we need to query the database for this information for 1, 7 and 56 days. This means I run the query 1 time, 7 times and 56 times, respectively. As the number goes up, the length of time for the query gets really long. I also have to run each of these queries for each failure type, which really adds to the query time.

Is there a more efficient way to query the database? So far, the closest question I've found on Stack was this one:

Note: I am running this query in a Flask-based web app and then graphing the results.

Community
  • 1
  • 1
Mike Driscoll
  • 32,629
  • 8
  • 45
  • 88

1 Answers1

1

if you post a sample output it would help us to understand more. You can group by failure type and put a date interval in your where clause.

something like:

SELECT * from tbl_failures
WHERE (cause = '{fail_type} failed') AND (date_field between date_sub(curdate(), interval 7 day) and curdate()))
Group by tbl_failures.Type

Same query for 7 days chart:

SELECT * from tbl_failures
WHERE (cause = '{fail_type} failed') AND (date_field between date_sub(curdate(), interval 1 week) and curdate()))
Group by tbl_failures.Type

and so on..

EDIT have added date range. With date between you can look back into a specific range like one week, one month excluding everything else.

Krish
  • 5,917
  • 2
  • 14
  • 35