我試圖分解並重新編寫由已離開的開發人員創建的視圖。該查詢需要三個以上的minuites訪問,我從所有的CONCATs假設。SQL VIEW簡化/解決方案更快Querys
CREATE VIEW `active_users_over_time` AS
select
`users_activity`.`date` AS `date`,
time_format(
addtime(
concat(`users_activity`.`date`,' ',`users_activity`.`time`),
concat('0 ',sec_to_time(`users_activity`.`duration_checkout`),'.0')
),'%H:%i:%s') AS `time`,
`users_activity`.`username` AS `username`,
count(addtime(concat(`users_activity`.`date`,' ',`users_activity`.`time`),
concat('0 ',sec_to_time(`users_activity`.`duration_checkout`),'.0'))) AS `checkouts`
from `users_activity`
group by
concat(
addtime(
concat(`users_activity`.`date`,' ',`users_activity`.`time`),
concat('0 ',sec_to_time(`users_activity`.`duration_checkout`),'.0')
),
`users_activity`.`username`);
的數據來自SQL表:
CREATE TABLE `users_activity` (
`id` int(10) unsigned NOT NULL auto_increment,
`featureid` smallint(5) unsigned NOT NULL,
`date` date NOT NULL,
`time` time NOT NULL,
`duration_checkout` int unsigned NOT NULL,
`update_date` date NOT NULL,
`username` varchar(255) NOT NULL,
`checkout` smallint(5) unsigned NOT NULL,
`licid` smallint(5) unsigned NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `featureid_licid_username` (`featureid`,`licid`,`date`,`time`,`username`),
FOREIGN KEY(featureid) REFERENCES features(id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
我有一個很難decifering正是心不是需要什麼需要什麼和什麼。
任何人有任何想法?謝謝。
它來自一張表嗎?你可以從視圖中運行選擇,看看它是如何執行的?並將其與查詢的簡化版本(沒有所有連接等)進行比較。 – Andrew
我不知道mysql是否具有此功能,但是我會爲您在sql server中連接的內容創建持久性計算列。然後,您只需在輸入或更改數據時進行計算,而不是每次需要獲取數據。 – HLGEM
沒有什麼看起來真的太重了。如果您運行查詢,視圖正在執行,是否需要運行相同的三分鐘?我幾乎要回到需要......他究竟想用那麼大的time_formt來顯示(線? – Twelfth