0

I would really appreciate it if some of you could help optimize my tables, as they currently take extremely long to execute because of the following issues:

Main table:

game {
   game_id [PRIMARY KEY]
   *team_id
   *stadium_id
   home_score
   opponent_score
   date
   tour
   half_home_score
   half_opp_score
   attendance
   referee_id
   coach_id
   captain_id
}

players (contains all players that played in the game) {
    *game_id
    *team_id
    *player_id
    position
    number
}

tries, conversions, penalties, dropgoals {
    *player_id
    *game_id
    *team_id
}

team {
    team_id [PRIMARY KEY]
    country
}

player {
    player_id [PRIMARY KEY]
    *team_id
    name
    surname
    fullname
    DOB
    POB
    school
    height
    weight
    position
}

I tend to make all *_id's of type (UNSIGNED INT[5])? I am a bit unsure if UNSIGNED is needed

All text is VARCHAR(500..200) depending on the size needed

I use DATE type where I can eg. DOB, date

' * ' - refers to foreign keys that are primary keys in other tables

One page is particularly slow, what happens is the following:

The page shows the lineup of players for the specific game so my queries are the following:

SELECT all player_id's,number,position FROM players table WHERE game_id is specific game's id getTries(player_id,game_id) from tries table . . getDropgoals(player_id,game_id) from dropgoals table

getPlayerName(player_id) from player table

Output to table the received details

<tr>
<td>tries</td>...<td>dropgoals</td><td>position</td><td>player's name</td><td>number</td></tr>

I would really appreciate it if someone could point out some visible pitfalls.

Regards

// edit

I have used the following query, but it only outputs the rows that found in the tries table, I want it to output all players found, but only count the number of tries that he scored, if no tries were scored for that player, it must still output the players details but 0 for tries scored.

I am not sure if my query is correct: SELECT ps.player_id, ps.position, ps.number, p.name, p.surname, COUNT(*) AS triesNo FROM players ps, player p, tries WHERE p.player_id=ps.player_id AND ps.game_id = '$game_id' AND ps.team_id IS NULL AND tries.player_id = ps.player_id GROUP BY ps.player_id ORDER BY ps.number

I want it to also return a player if he scored no tries, now it only returns the player if he scored a try.

Can anyone help please?

4

2 回答 2

1

从它的声音来看,如果您一次从多个表中进行选择,您可能 (a) 在页面加载时运行了太多查询,或者 (b) 没有在适当的字段中加入数据。

例如,从您所说的来看,您似乎正在运行一整套查询来获取玩家姓名,但您可以将其与您的第一个查询合并,如下所示:

SELECT ps.player_id, ps.position, ps.number, p.name
FROM players ps, player p
WHERE p.player_id=ps.player_id

该查询连接了 player_id 上的两个表,最终得到一个包含 id/position/number/name 的玩家数组。

您可能还想查看索引。索引 WHERE 子句中使用的任何字段(尚未使用主键索引)是一个好主意。

正如其他人所说,您需要更具体地了解哪些查询运行缓慢。

于 2009-07-08T16:43:48.717 回答
0

无符号将使您拥有更大(两倍大小)的 int id 列。缺点是您将无法使用负 ID(无论如何,这是邪恶的)。

于 2009-07-08T16:13:55.127 回答