私は従来のデータベース構造に拘束されており、いくつかの統計結果が必要です。次のクエリは機能しますが、非効率的で遅いです...
SELECT various, other, native, columns,
(SELECT client FROM clients WHERE id = clientid) AS client,
(SELECT name FROM categories WHERE id = (SELECT categoryid FROM clients WHERE id = clientid)) AS category,
(SELECT fullname FROM staff WHERE id = producerid) AS producer,
ISNULL((SELECT SUM(amount) FROM JobsVoiceWork v WHERE v.jobid = j.id),0) AS voicecosts,
(SELECT COUNT(*) FROM Scripts s WHERE s.jobid = j.id) AS numberofscriptscompleted,
ISNULL((SELECT SUM(duration) FROM TimeLog WHERE jobid = j.id),0)/60 AS totaltime,
ISNULL((SELECT SUM(duration) FROM TimeLog WHERE jobid = j.id AND jobeditid = 3 AND jobpart = 'Add'),0)/60 AS PartAdd,
ISNULL((SELECT SUM(duration) FROM TimeLog WHERE jobid = j.id AND jobeditid = 3 AND jobpart = 'Update'),0)/60 AS PartUpdate,
ISNULL((SELECT SUM(duration) FROM TimeLog WHERE jobid = j.id AND jobeditid = 3 AND jobpart = 'Produce'),0)/60 AS PartProduce,
ISNULL((SELECT SUM(duration) FROM TimeLog WHERE jobid = j.id AND jobeditid = 3 AND jobpart = 'Amend'),0)/60 AS PartAmend,
ISNULL((SELECT SUM(duration) FROM TimeLog WHERE jobid = j.id AND jobeditid = 4),0)/60 AS EditProducerError,
ISNULL((SELECT SUM(duration) FROM TimeLog WHERE jobid = j.id AND jobeditid = 8),0)/60 AS EditVoiceError,
ISNULL((SELECT SUM(duration) FROM TimeLog WHERE jobid = j.id AND jobeditid = 1),0)/60 AS EditClientError,
ISNULL((SELECT SUM(duration) FROM TimeLog WHERE jobid = j.id AND jobeditid = 2),0)/60 AS EditEntryError,
ISNULL((SELECT SUM(duration) FROM TimeLog WHERE jobid = j.id AND jobeditid = 5),0)/60 AS EditPronunciation,
ISNULL((SELECT SUM(duration) FROM TimeLog WHERE jobid = j.id AND jobeditid = 6),0)/60 AS EditRemixRequest,
ISNULL((SELECT SUM(duration) FROM TimeLog WHERE jobid = j.id AND jobeditid = 7),0)/60 AS EditRevoiceRequest
FROM Jobs j
クエリの簡略化したバージョンを示しましたが、非効率性を明確に示すために繰り返しのサブクエリを含めました。さまざまなテーブル結合シナリオを試しましたが、パフォーマンスを改善できません。
改善できるはずのようです。方法はありますか?