2017-02-09 65 views
0

今天我的注意力不集中,或者Slick的入門文檔沒有很好的設計。 所以在查詢/工會節他們有這樣的片段:Slick文檔中的錯誤示例?

val q1 = coffees.filter(_.price < 8.0) 
val q2 = coffees.filter(_.price > 9.0) 

val unionQuery = q1 union q2 
// compiles to SQL (simplified): 
// select x8."COF_NAME", x8."SUP_ID", x8."PRICE", x8."SALES", x8."TOTAL" 
//  from "COFFEES" x8 
//  where x8."PRICE" < 8.0 
// union select x9."COF_NAME", x9."SUP_ID", x9."PRICE", x9."SALES", x9."TOTAL" 
//  from "COFFEES" x9 
//  where x9."PRICE" > 9.0 

val unionAllQuery = q1 ++ q2 
// compiles to SQL (simplified): 
// select x8."COF_NAME", x8."SUP_ID", x8."PRICE", x8."SALES", x8."TOTAL" 
//  from "COFFEES" x8 
//  where x8."PRICE" < 8.0 
// union all select x9."COF_NAME", x9."SUP_ID", x9."PRICE", x9."SALES", x9."TOTAL" 
//  from "COFFEES" x9 
//  where x9."PRICE" > 9.0 

然後他們說: Unlike union which filters out duplicate values, ++ simply concatenates the results of the individual queries, which is usually more efficient.

我覺得有沒有通過q1q2產生重複。所以這是一個錯誤的查詢例子,他們提供了說明union++之間的真正區別,或者我沒有看到重要的東西。你們能幫忙嗎?

回答

1

在這種特定情況下,沒有重複項,因爲q1q2之間沒有交集。也許改變查詢

val q1 = coffees.filter(_.price < 8.0) 
val q2 = coffees.filter(_.price < 9.0) 

本來是一個更好的例子。總之,的底線是:

  • q1 union q2轉換爲SQL UNION
  • q1 ++ q2轉換爲SQL UNION ALL