前段時間在實際工作中,使用者的一條sql引發了我一些思考。寫一篇簡單的博文來記錄下。實際表的列名等已替換。
select
*from
db1.table1 as t1
left
join db2.table2 as t2 on
t1.col1 = t2.col2
where
t1.time
>=
'2020-01-01 00:00:00'
and t1.
time
<
'2020-01-31 00:00:00'
and t1.col2 in
('5k'
)
這條sql語句在clickhouse19.18.1.1版本會報如下異常(這是clickhouse自身的乙個bug,在20.3.2.1版本中已修復,2020-03-12)
code: 48. db::exception: received from localhost:10115. db::exception:method createcolumn() is not implemented for data type set.
select
*from
db1.table1 as t1
left
join db2.table2 as t2 on
t1.col1 = t2.col2
where
t1.time
>=
'2020-01-01 00:00:00'
and t1.
time
<
'2020-01-31 00:00:00'
select
*from
db1.table1 as t1
left
join db2.table2 as t2 on
t1.col1 = t2.col2
where
t1.col2 in
('5k'
)
無論上面那種情況都不再報錯了。再結合下異常錯誤提示,我們可以推測,一定是join時,對字段的推導有問題了。那麼重點來了,在不公升級版本或者乾脆這個bug就沒修復,客戶這邊也不想大幅度修改sql,那我們又該怎麼處理?
通過分析,這條sql就是一條join語句,並且過濾條件全部是一張表的列,那麼我們是否可以先利用過濾字段查出一張自表之後,再去join呢?帶著這樣的思路,我們修改sql如下:
select
*from
(select
*from db1.table1 where col2 in
('5k'))
as t1
left
join db2.table2 as t2 on
t1.col1 = t2.col2
where
t1.time
>=
'2020-01-01 00:00:00'
and t1.
time
<
'2020-01-31 00:00:00'
這次我們發現sql不再報錯了。接著對於clickhouse來說,不要使用select * 這樣的影響效能的查詢(雖然這樣也很快),我們把過濾條件中用到的列查出來即可。sql改動如下:
select
*from
(select col1,col2,
time
from db1.table1 where col2 in
('5k'))
as t1
left
join db2.table2 as t2 on
t1.col1 = t2.col2
where
t1.time
>=
'2020-01-01 00:00:00'
and t1.
time
<
'2020-01-31 00:00:00'
到這裡我們已經利用子查詢,解決了之前的問題。但我們還想再改,我們希望能盡可能跟客戶原先的sql一致,於是我們這次把子查詢中的條件再次拿到外層,如下:
select
*from
(select col1,col2,
time
from db1.table1)
as t1
left
join db2.table2 as t2 on
t1.col1 = t2.col2
where
t1.time
>=
'2020-01-01 00:00:00'
and t1.
time
<
'2020-01-31 00:00:00'
and t1.col2 in
('5k'
)
上一條記錄下一條記錄
select top 1 from 表 where id 當前id order by id desc select top 1 from 表 where id 當前id order by id desc 上一條記錄 select top 1 blogid from gcc bloginfo wher...
顯示上一條新聞 下一條新聞
假設當前newsid 2 select newstitle from newstable where newsid select top 1 newsid from newstable where newsid 2 order by newsid asc or newsid select top 1...
內容迴圈滾動 內容一條一條顯示
class m quote newst class count style color 777c7c 今天已有 位業主發布招標 class quote list style border 1px solid ebebeb background fff class list tit style bor...