sql - Can I make this MySQL staement with complex COUNTs more efficient? -
I have a table of all major league baseball games that is structured like this:
** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** Tickets sold for different games: ** ticket ** id (int) game_id (int) price (float) time_sold (datetime)
D like running a MySQL query that every ticket is The match matches the home and away team records for the time being the time the ticket was sold I am using it:
SELECT ticket.id, game .home_team_id AS home, game.away_team_id as Away, (SELECT COUNT (game.id) from game WHERE game.date & lt; DATE_SUB (ticket.time_sold, INTERVAL 1 day) and (game.home_score> game.away_score and game. Home_team = home or game.away_score> game.home_score and game.away_team = away) home_team_wins game.id = ticket.game_id
on the JOIN game by ticket I know that the query is extremely slow, I have a price tag in all the columns and ticket tables in the game table. Also has indexed all, but still slow pain.
Can someone suggest how can I move it?
I think that the selection of the scalar in the selector section can be a subquaire criminal, in the FROM section I try to move
Try it and see if it is helpful (I can have syntax errors, I have no environment to test today):
SELECT ticket Id, game.home_team_id as home, game.away_team_id as far, game_count FROM (SELECT COUNT (game.id) game_count game from WHOLE game.date & lt; DATE_SUB (ticket.time_sold, INTERVAL 1 day and (game.home_score> game Game.id = ticket.game_id at home_team_wins, ticket JOIN game as .ay_score and game.home_team = home or game.away_score> gt; game.home_score and game.away_team =)))
Comments
Post a Comment