How to convert 3 selects into one query



  • can someone help me turn this 3 querys into one?

    I gather that it would be faster and better than having the 3 select running all the time.

    this runs in python with flask and the db is mysql

    mycursor.execute('SELECT COUNT(*) FROM testes__db WHERE module = %s', (line[0],))
    

    mycursor.execute('SELECT COUNT(*) FROM result_testes__db a1 INNER JOIN testes__db a2 ON a1.req_id = a2.id and a2.module =%s and a1.test_id =%s',(int(line[0]),int(session['test_id']),))

    mycursor.execute('SELECT COUNT(*) FROM result_testes__db a1 INNER JOIN testes__db a2 ON a1.req_id = a2.id and a2.module =%s and a1.test_id =%s where eval=0',(int(line[0]), int(session['test_id']),))



  • can someone help me turn this 3 querys into one?

    SELECT COUNT(*) FROM testes__db WHERE module = %s
    UNION ALL
    SELECT COUNT(*) FROM result_testes__db a1 INNER JOIN testes__db a2 ON a1.req_id = a2.id and a2.module =%s and a1.test_id =%s
    UNION ALL
    SELECT COUNT(*) FROM result_testes__db a1 INNER JOIN testes__db a2 ON a1.req_id = a2.id and a2.module =%s and a1.test_id =%s where eval=0
    

    Then add all parameters... You'll save 2 database roundtrip times which is not much. If you want to overdo it, and "eval" is a bool, you could do:

    SELECT 0, COUNT(*) FROM testes__db WHERE module = %s
    UNION ALL
    SELECT eval, COUNT(*) FROM result_testes__db a1 INNER JOIN testes__db a2 ON a1.req_id = a2.id and a2.module =%s and a1.test_id =%s GROUP BY eval
    

    So you get the count for both values of "eval" in one query.

    I gather that it would be faster and better than having the 3 select running all the time.

    That's not necessarily the case.

    First, you should benchmark it. Back when I was doing websites, I added a query log at the bottom of the page showing all queries and their execution time. This was displayed only to a user logged in with admin credentials, of course, otherwise it would be a rather large security hole. This is a nice way to immediately spot slow queries while browsing the site.

    You can do this with a wrapper object on the database connection.

    Then once you know which queries are actually a problem, you can spend time optimizing them, or doing other things like caching the results.


Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2