1
我有一個sql查詢,我檢查一個值是否在表的最大值和最小值之間。現在我已經實現這個如下:如何以有效的方式使用min()和max()?
SELECT spectrum_id, feature_table_id
FROM 'spectrum', 'feature'
WHERE `spectrum`.msrun_msrun_id = 1
AND `feature`.msrun_msrun_id = 1
AND (SELECT min(rt) FROM `convexhull` WHERE `convexhull`.feature_feature_table_id = `feature`.feature_table_id) <= scan_start_time
AND scan_start_time <= (SELECT max(rt) FROM `convexhull` WHERE 'convexhull'.feature_feature_table_id = 'feature'.feature_table_id)
AND (SELECT min(mz) FROM `convexhull` WHERE `convexhull`.feature_feature_table_id = `feature`.feature_table_id) <= base_peak_mz
AND base_peak_mz <= (SELECT max(mz) FROM `convexhull` WHERE `convexhull`.feature_feature_table_id = `feature`feature_table_id)
這是運行速度很慢,因爲我從凸形輪廓選擇我每次運行此查詢時間爲4倍,所以我嘗試使用內,以改善其加入:
SELECT spectrum_id, feature_table_id
FROM 'spectrum', 'feature'
INNER JOIN `convexhull` ON `convexhull`.feature_feature_table_id = `feature`.feature_table_id
WHERE `spectrum`.msrun_msrun_id = ? "+
AND `feature`.msrun_msrun_id = ? "+
AND min(`convexhull`.rt) <= scan_start_time "+
AND scan_start_time <= max(`convexhull`.rt) "+
AND min(`convexhull`.mz) <= base_peak_mz "+
AND base_peak_mz <= max(`convexhull`.mz)", spectrumFeature_InputValues)
但是,min()和max()語句只能在select語句後使用。我怎樣才能使第一個查詢更高效,這樣我就可以得到最小和最大rt和mz而不必執行4個查詢?
我剛完成測試,你寫的最上面的部分,它使查詢快了3倍,謝謝!只要數據仍然正確,只要 – 2012-04-26 13:42:58
;-) – gordatron 2012-04-26 13:51:29