與組的最高時間戳這是我的查詢:使MySQL查詢選擇通過聲明
SELECT
log_timestamp,
system_name,
faction_name,
faction_allegiance,
faction_government_type,
current_state,
pending_state,
recovering_state,
influence AS influence,
transactions.transaction_id
FROM `bgs`.`faction_log`
INNER JOIN `factions` ON `factions`.`faction_id` = `faction_log`.`faction_id`
INNER JOIN `transactions` ON `transactions`.`transaction_id` = `faction_log`.`transaction_id`
INNER JOIN `system_log` ON `system_log`.`transaction_id` = `transactions`.`transaction_id`
INNER JOIN `systems` ON `systems`.`system_id` = `transactions`.`associated_system_id`
WHERE
log_timestamp BETWEEN bgs.find_closest_tick_time(date('2017-08-14')) AND bgs.find_closest_tick_time(DATE_ADD(date('2017-08-14'), INTERVAL 1 DAY)) AND
associated_system_id = 4
GROUP BY
faction_name,
log_timestamp,
system_name,
faction_allegiance,
faction_government_type,
current_state,
pending_state,
recovering_state,
influence,
transactions.transaction_id
它工作正常,現在看起來是這樣的:
+---------------------+-------------+-----------------+ | log_timestamp | system_name | faction_name | +---------------------+-------------+-----------------+ | 2017-08-14 11:18:46 | Nagii | Nagii Autocracy | | 2017-08-14 12:38:03 | Nagii | Nagii Autocracy | | 2017-08-14 13:40:18 | Nagii | Nagii Autocracy | | 2017-08-15 06:09:52 | Nagii | Nagii Autocracy | | 2017-08-14 11:18:46 | Nagii | Nagii Network | | 2017-08-14 12:38:03 | Nagii | Nagii Network | | 2017-08-14 13:40:18 | Nagii | Nagii Network | | 2017-08-15 06:09:52 | Nagii | Nagii Network | | 2017-08-14 11:18:46 | Nagii | Nagii Posse | | 2017-08-14 12:38:03 | Nagii | Nagii Posse | | 2017-08-14 13:40:18 | Nagii | Nagii Posse | | 2017-08-15 06:09:52 | Nagii | Nagii Posse | | 2017-08-14 11:18:46 | Nagii | Nagii Union | | 2017-08-14 12:38:03 | Nagii | Nagii Union | | 2017-08-14 13:40:18 | Nagii | Nagii Union | | 2017-08-15 06:09:52 | Nagii | Nagii Union | | 2017-08-14 11:18:46 | Nagii | Nagii United Co | | 2017-08-14 12:38:03 | Nagii | Nagii United Co | | 2017-08-14 13:40:18 | Nagii | Nagii United Co | | 2017-08-15 06:09:52 | Nagii | Nagii United Co | +---------------------+-------------+-----------------+ 20 rows in set (0.00 sec)
但我想唯一選擇包含字段log_timestamp的最大值的行。 (在這種情況下2017-08-15 06:09:52)。我試圖用類似的東西:
HAVING transactions.log_timestamp >= max(date('2017-08-15 06:09:52'));
,但它不喜歡的工作:
HAVING transactions.log_timestamp >= max(log_timestamp);
我到底做錯了什麼?
什麼是最適合大桌子上的性能? –
性能最好的方法是使用索引優化表。您沒有爲您的表發佈「SHOW CREATE TABLE」,也不知道「log_timestamp」屬於哪個表,因此我無法評論哪些索引是需要的。 –