2017-10-20 56 views
1

我在x86_64使用mysql 版本8.0.3-RC爲Linux(MySQL社區服務器(GPL))MySQL的8個窗口功能+全文搜索

列上創建表和全文索引

CREATE TABLE `title` (
    `id` smallint(4) unsigned NOT NULL PRIMARY KEY, 
    `name` text COLLATE utf8_unicode_ci, 
    FULLTEXT idx (name) WITH PARSER ngram 
) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 

插入一些數據:

insert into `title` values(14,"I'm flying in for the game (one night in Niagara Falls, NY and one night in Buffalo then back home)."); 
insert into `title` values(23,"I've never been to the area."); 
insert into `title` values(43,"Where and what must I eat (Canadian side of Niagara, American side and Buffalo)?"); 
insert into `title` values(125,"Don't really have much planned other than the Falls and the game."); 

當執行:

select 
    id, 
    round(MATCH (name) AGAINST ('other than the'),2) scope 
from title; 

結果(一切OK):

id | scope 
---------- 
14 | 0.43 
23 | 0.23 
43 | 0.12 
125 | 1.15 

當使用經典GROUP BY - 一切ok

select 
    max(scope), 
    min(scope), 
    sum(scope) 
from 
(
    select id, round(MATCH (name) AGAINST ('other than the'),2) scope 
    from title 
) a; 

結果確定:

max | min | sum 
---------------- 
1.15 | 0.12 | 1.96 

但是當我嘗試使用窗口功能我不明白的結果:

select 
    id, 
    max(scope) over(), 
    min(scope) over(), 
    sum(scope) over() 
from 
(
    select id, round(MATCH (name) AGAINST ('other than the'),2) scope 
    from title 
) a; 

我得到一個奇怪的結果(爲什麼?):

id | max | min | sum 
------------------------ 
14 | 1.15 | 1.15 | 4.60 
23 | 1.15 | 1.15 | 4.60 
43 | 1.15 | 1.15 | 4.60 
125| 1.15 | 1.15 | 4.60 

我希望通過得到類似古典組的結果,如:

id | max | min | sum 
------------------------ 
14 | 1.15 | 0.12 | 1.96 
23 | 1.15 | 0.12 | 1.96 
43 | 1.15 | 0.12 | 1.96 
125| 1.15 | 0.12 | 1.96 

這是在MySQL 版本8.0.3-RC或不正確我查詢了錯誤? 謝謝!

+0

你的結果對我來說似乎很陌生。我也希望你能得到相同的輸出。 –

+0

你的觀察是正確的。這是8.0.3-rc發佈後修復的錯誤。 -Dag(MySQL dev) – DagW

回答

0

關於wchiquito的回答:你是對的,有一個錯誤。自發布以來它已經修復。修復之後,MySQL將這個答案返回給窗口查詢:

mysql> SELECT 
    ->  `id`, 
    ->  MAX(`scope`) OVER() `max`, 
    ->  MIN(`scope`) OVER() `min`, 
    ->  SUM(`scope`) OVER() `sum` 
    ->  FROM 
    ->  (
    ->  SELECT 
    ->   `id`, 
    ->   ROUND(MATCH (`name`) AGAINST ('other than the'), 2) `scope` 
    ->  FROM `title` 
    ->  ) `a`; 
+-----+------+------+------+ 
| id | max | min | sum | 
+-----+------+------+------+ 
| 14 | 0.72 | 0.00 | 0.72 | 
| 23 | 0.72 | 0.00 | 0.72 | 
| 43 | 0.72 | 0.00 | 0.72 | 
| 125 | 0.72 | 0.00 | 0.72 | 
+-----+------+------+------+ 
4 rows in set (0,01 sec) 

這與您從瑪麗亞引用的不同,但我相信 上面的MySQL答案是正確的:由於窗口規範是空的,因此窗口函數應該對每行的結果集中的所有行進行操作,即每個窗口函數調用的結果應該是相同的值結果集線。

如果劃分結果同樣地設定什麼是集團做BY查詢(見PARTITION BY a.id在下面),你會看到這樣的結果:

mysql> SELECT 
    ->  `id`, 
    ->  MAX(`scope`) OVER(PARTITION BY a.id) `max`, 
    ->  MIN(`scope`) OVER(PARTITION BY a.id) `min`, 
    ->  SUM(`scope`) OVER(PARTITION BY a.id) `sum` 
    ->  FROM 
    ->  (
    ->  SELECT 
    ->   `id`, 
    ->   ROUND(MATCH (`name`) AGAINST ('other than the'), 2) `scope` 
    ->  FROM `title` 
    ->  ) `a`; 
+-----+------+------+------+ 
| id | max | min | sum | 
+-----+------+------+------+ 
| 14 | 0.00 | 0.00 | 0.00 | 
| 23 | 0.00 | 0.00 | 0.00 | 
| 43 | 0.00 | 0.00 | 0.00 | 
| 125 | 0.72 | 0.72 | 0.72 | 
+-----+------+------+------+ 
4 rows in set (0,00 sec) 

,因爲每一行都是自己的在這裏分區。這與您引用Maria 時沒有使用PARTITION BY相同。

+0

我已經更新了[answer](https://stackoverflow.com/a/46848004/1316440)。當然,這一直是我的一個疏忽,MariaDB部分的第三個'SELECT'返回預期結果,請參見[dbfiddle](http://dbfiddle.uk/?rdbms=mariadb_10.2&fiddle=88d4e48eac1bee76e576ec883af781c9)。謝謝。 – wchiquito

0

看起來你已經在MySQL中發現了一個bug,報告bug:bugs.mysql.com

我執行在MySQL和MariaDB的下面的腳本(不WITH PARSER ngram因爲目前MariaDB的它不支持,請參閱Add "ngram" support to MariaDB)與結果:

的MySQL:

mysql> SELECT VERSION(); 
+--------------+ 
| VERSION() | 
+--------------+ 
| 8.0.3-rc-log | 
+--------------+ 
1 row in set (0.00 sec) 

mysql> DROP TABLE IF EXISTS `title`; 
Query OK, 0 rows affected (0.02 sec) 

mysql> CREATE TABLE `title` (
    -> `id` SMALLINT UNSIGNED NOT NULL PRIMARY KEY, 
    -> `name` TEXT COLLATE utf8_unicode_ci, 
    -> FULLTEXT idx (`name`) -- WITH PARSER ngram 
    ->) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 
Query OK, 0 rows affected (0.01 sec) 

mysql> INSERT INTO `title` 
    -> VALUES 
    -> (14, "I'm flying in for the game (one night in Niagara Falls, NY and one night in Buffalo then back home)."), 
    -> (23, "I've never been to the area."), 
    -> (43, "Where and what must I eat (Canadian side of Niagara, American side and Buffalo)?"), 
    -> (125, "Don't really have much planned other than the Falls and the game."); 
Query OK, 4 rows affected (0.00 sec) 
Records: 4 Duplicates: 0 Warnings: 0 

mysql> SELECT 
    -> MAX(`scope`), 
    -> MIN(`scope`), 
    -> SUM(`scope`) 
    -> FROM 
    -> (
    -> SELECT 
    ->  `id`, 
    ->  ROUND(MATCH (`name`) AGAINST ('other than the'), 2) `scope` 
    -> FROM `title` 
    ->) `a`; 
+--------------+--------------+--------------+ 
| MAX(`scope`) | MIN(`scope`) | SUM(`scope`) | 
+--------------+--------------+--------------+ 
|   0.72 |   0.00 |   0.72 | 
+--------------+--------------+--------------+ 
1 row in set (0.00 sec) 

mysql> SELECT 
    -> `id`, 
    -> MAX(`scope`) OVER(), 
    -> MIN(`scope`) OVER(), 
    -> SUM(`scope`) OVER() 
    -> FROM 
    -> (
    -> SELECT 
    ->  `id`, 
    ->  ROUND(MATCH (`name`) AGAINST ('other than the'), 2) `scope` 
    -> FROM `title` 
    ->) `a`; 
+-----+---------------------+---------------------+---------------------+ 
| id | MAX(`scope`) OVER() | MIN(`scope`) OVER() | SUM(`scope`) OVER() | 
+-----+---------------------+---------------------+---------------------+ 
| 14 |    0.72 |    0.72 |    2.88 | 
| 23 |    0.72 |    0.72 |    2.88 | 
| 43 |    0.72 |    0.72 |    2.88 | 
| 125 |    0.72 |    0.72 |    2.88 | 
+-----+---------------------+---------------------+---------------------+ 
4 rows in set (0.00 sec) 

MariaDB的:

MariaDB[_]> SELECT VERSION(); 
+----------------------------------------+ 
| VERSION()        | 
+----------------------------------------+ 
| 10.2.6-MariaDB-10.2.6+maria~jessie-log | 
+----------------------------------------+ 
1 row in set (0.00 sec) 

MariaDB[_]> DROP TABLE IF EXISTS `title`; 
Query OK, 0 rows affected (0.02 sec) 

MariaDB[_]> CREATE TABLE `title` (
     -> `id` SMALLINT UNSIGNED NOT NULL PRIMARY KEY, 
     -> `name` TEXT COLLATE utf8_unicode_ci, 
     -> FULLTEXT idx (`name`) -- WITH PARSER ngram 
     ->) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 
Query OK, 0 rows affected (0.01 sec) 

MariaDB[_]> INSERT INTO `title` 
     -> VALUES 
     -> (14, "I'm flying in for the game (one night in Niagara Falls, NY and one night in Buffalo then back home)."), 
     -> (23, "I've never been to the area."), 
     -> (43, "Where and what must I eat (Canadian side of Niagara, American side and Buffalo)?"), 
     -> (125, "Don't really have much planned other than the Falls and the game."); 
Query OK, 4 rows affected (0.00 sec) 
Records: 4 Duplicates: 0 Warnings: 0 

MariaDB[_]> SELECT 
     -> MAX(`scope`), 
     -> MIN(`scope`), 
     -> SUM(`scope`) 
     -> FROM 
     -> (
     -> SELECT 
     ->  `id`, 
     ->  ROUND(MATCH (`name`) AGAINST ('other than the'), 2) `scope` 
     -> FROM `title` 
     ->) `a`; 
+--------------+--------------+--------------+ 
| MAX(`scope`) | MIN(`scope`) | SUM(`scope`) | 
+--------------+--------------+--------------+ 
|   0.72 |   0.00 |   0.72 | 
+--------------+--------------+--------------+ 
1 row in set (0.00 sec) 

MariaDB[_]> SELECT 
     -> `id`, 
     -> MAX(`scope`) OVER(), 
     -> MIN(`scope`) OVER(), 
     -> SUM(`scope`) OVER() 
     -> FROM 
     -> (
     -> SELECT 
     ->  `id`, 
     ->  ROUND(MATCH (`name`) AGAINST ('other than the'), 2) `scope` 
     -> FROM `title` 
     ->) `a`; 
+-----+--------------+--------------+--------------+ 
| id | MAX(`scope`) | MIN(`scope`) | SUM(`scope`) | 
+-----+--------------+--------------+--------------+ 
| 14 |   0.72 |   0.00 |   0.72 | 
| 23 |   0.72 |   0.00 |   0.72 | 
| 43 |   0.72 |   0.00 |   0.72 | 
| 125 |   0.72 |   0.00 |   0.72 | 
+-----+--------------+--------------+--------------+ 
4 rows in set (0.00 sec)