1. 程式人生 > >Solr查詢與sql對比轉化

Solr查詢與sql對比轉化


將常用sql查詢轉化為Solr查詢,網上已有做出對比的,這裡引用一下,去掉部分圖片,快速對比,

原文出處:http://shiyanjun.cn/archives/78.html


查詢對比:
-------------------------------------------------------------------------------------------------------------------------------------------
1、條件組合查詢
SQL查詢語句:
SELECT log_id,start_time,end_time,prov_id,city_id,area_id,idt_id,cnt,net_type
FROM v_i_event
WHERE prov_id = 1 AND net_type = 1 AND area_id = 10304 AND time_type = 1 AND time_id >= 20130801 AND time_id <= 20130815
ORDER BY log_id LIMIT 10;


Solr查詢URL:
http://192.x.x.x:port/xxx/core1/select?q=*:*&fl=log_id,start_time,end_time,prov_id,city_id,area_id,idt_id,cnt,net_type&fq=prov_id:1 AND net_type:1 AND area_id:10304 AND time_type:1 AND time_id:[20130801 TO 20130815]&sort=log_id asc&start=0&rows=10




2、單個欄位分組統計

SELECT prov_id, SUM(cnt) AS sum_cnt, AVG(cnt) AS avg_cnt, MAX(cnt) AS max_cnt, MIN(cnt) AS min_cnt, COUNT(cnt) AS count_cnt
FROM v_i_event
GROUP BY prov_id;

Solr查詢URL:
http://192.x.x.x:port/xxx/core1/select?q=*:*&stats=true&stats.field=cnt&rows=0&indent=true




3、IN條件查詢
SQL查詢語句:
SELECT log_id,start_time,end_time,prov_id,city_id,area_id,idt_id,cnt,net_typ
FROM v_i_event
WHERE prov_id = 1 AND net_type = 1 AND city_id IN(106,103) AND idt_id IN(12011,5004,6051,6056,8002) AND time_type = 1 AND time_id >= 20130801 AND time_id <= 20130815
ORDER BY log_id, start_time DESC LIMIT 10;


Solr查詢URL:
http://192.x.x.x:port/xxx/core1/select?q=*:*&fl=log_id,start_time,end_time,prov_id,city_id,area_id,idt_id, cnt,net_type&fq=prov_id:1 AND net_type:1 AND (city_id:106 OR city_id:103) AND (idt_id:12011 OR idt_id:5004 OR idt_id:6051 OR idt_id:6056 OR idt_id:8002) AND time_type:1 AND time_id:[20130801 TO 20130815]&sort=log_id asc ,start_time desc&start=0&rows=10



http://192.x.x.x:port/xxx/core1/select?q=*:*&fl=log_id,start_time,end_time,prov_id,city_id,area_id,idt_id, cnt ,net_type&fq=prov_id:1&fq=net_type:1&fq=(city_id:106 OR city_id:103)&fq=(idt_id:12011 OR idt_id:5004 OR idt_id:6051 OR idt_id:6056 OR idt_id:8002)&fq=time_type:1&fq=time_id:[20130801 TO 20130815]&sort=log_id asc,start_time desc&start=0&rows=10



4、開區間範圍條件查詢

SQL查詢語句:
SELECT log_id,start_time,end_time,prov_id,city_id,area_id,idt_id,cnt,net_type
FROM v_i_event
WHERE net_type = 1 AND idt_id IN(12011,5004,6051,6056,8002) AND time_type = 1 AND start_time >= 1373598465 AND end_time < 1374055254
ORDER BY log_id, start_time, idt_id DESC LIMIT 30;




Solr查詢URL:
http://192.x.x.x:port/xxx/core1/select?q=*:*&fl=log_id,start_time,end_time,prov_id,city_id,area_id,idt_id,cnt,net_type&fq=net_type:1 AND (idt_id:12011 OR idt_id:5004 OR idt_id:6051 OR idt_id:6056 OR idt_id:8002) AND time_type:1 AND start_time:[1373598465 TO 1374055254]&fq =-start_time:1374055254&sort=log_id asc,start_time asc,idt_id desc&start=0&rows=30

http://192.x.x.x:port/xxx/core1/select?q=*:*&fl=log_id,start_time,end_time,prov_id,city_id,area_id,idt_id,cnt,net_type&fq=net_type:1 AND (idt_id:12011 OR idt_id:5004 OR idt_id:6051 OR idt_id:6056 OR idt_id:8002) AND time_type:1 AND start_time:[1373598465 TO 1374055254] AND -start_time:1374055254&sort=log_id asc,start_time asc,idt_id desc&start=0&rows=30

http://192.x.x.x:port/xxx/core1/select?q=*:*&fl=log_id,start_time,end_time,prov_id,city_id,area_id,idt_id,cnt,net_type&fq=net_type:1&fq=idt_id:12011 OR idt_id:5004 OR idt_id:6051 OR idt_id:6056 OR idt_id:8002&fq =time_type:1&fq=start_time:[1373598465 TO 1374055254]&fq =-start_time:1374055254&sort=log_id asc,start_time asc,idt_id desc&start=0&rows=30






5、多個欄位分組統計(只支援count函式)

SQL查詢語句:
SELECT city_id, area_id, COUNT(cnt) AS count_cnt
FROM v_i_event
WHERE prov_id = 1 AND net_type = 1
GROUP BY city_id, area_id;

Solr查詢URL:
http://192.x.x.x:port/xxx/core1/select?q=*:*&facet=true&facet.pivot=city_id,area_id&fq=prov_id:1 AND net_type:1&rows=0&indent=true





6、多個欄位分組統計(支援count、sum、max、min等函式)
一次對多個欄位進行獨立分組統計,Solr可以很好的支援。這相當於執行兩個帶有GROUP BY子句的SQL,這兩個GROUP BY分別只對一個欄位進行彙總統計。
SQL查詢語句:
SELECT city_id, area_id, COUNT(cnt) AS count_cnt
FROM v_i_event
WHERE prov_id = 1 AND net_type = 1
GROUP BY city_id;
 
SELECT city_id, area_id, COUNT(cnt) AS count_cnt
FROM v_i_event
WHERE prov_id = 1 AND net_type = 1
GROUP BY area_id;


Solr查詢URL:
http://192.x.x.x:port/xxx/core1/select?q=*:*&stats=true&stats.field=cnt&f.cnt.stats.facet=city_id&&f.cnt.stats.facet=area_id&fq=prov_id:1 AND net_type:1&rows=0&indent=true




7、多個欄位聯合分組統計(支援count、sum、max、min等函式)
SQL查詢語句:
SELECT city_id, area_id, SUM(cnt) AS sum_cnt, AVG(cnt) AS avg_cnt, MAX(cnt) AS max_cnt, MIN(cnt) AS min_cnt, COUNT(cnt) AS count_cnt
FROM v_i_event
WHERE prov_id = 1 AND net_type = 1
GROUP BY city_id, area_id;


Solr查詢URL:
http://localhost:8983/solr/solr_select/select?q=*:*&stats=true&stats.field=id&rows=0&indent=true