2017-10-20 5 views
1

Je Majeures Ver 8.0.3-rc pour Linux sur x86_64 (MySQL Community Server (GPL))MySQL 8 Fonctions de fenêtre + texte intégral recherche

Créer une table et l'index de texte intégral sur la colonne Nom

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; 

Insérer des données:

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."); 

Quand exécuter:

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

Résultat (tout ok):

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

Lorsque l'utilisation classique GROUP BY - tous ok

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

résultat ok:

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

Mais quand je tente utiliser la fonction de la fenêtre sur Je ne comprends pas:

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; 

-je obtenir un résultat étrange (POURQUOI?):

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 

J'espère obtenir des résultats similaires au groupe classique par, comme:

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 

Est-ce un bogue dans MySQL Ver-8.0.3 rc ou incorrect ma requête? Merci!

+0

Votre résultat me semble étrange. Je m'attendrais également à la même sortie que vous attendez. –

+0

Votre observation est correcte. C'est un bug qui a été corrigé après la sortie de 8.0.3-rc. -Dag (MySQL dev) – DagW

Répondre

0

En ce qui concerne la réponse de wchiquito: Vous avez raison, il y a un bug. Il a été corrigé depuis la publication. Après le correctif, MySQL renvoie cette réponse à la requête de fenêtrage:

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) 

qui est encore différent de celui que vous citez de Maria; mais la réponse MySQL ci-dessus est correcte: puisque la spécification de la fenêtre est vide, la fonction de la fenêtre devrait agir sur toutes les lignes du jeu de résultats pour chaque ligne, c'est-à-dire que la même ligne de jeu de résultats.

Si vous divisez le jeu de résultats de façon similaire à ce qui se fait pour la requête GROUP BY (voir PARTITION PAR a.id en ci-dessous), vous verrez ce résultat:

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) 

parce que chaque ligne est son propre partition ici. Ceci est le même que vous citez pour Maria sans la partition PAR.

+0

J'ai déjà mis à jour la [réponse] (https://stackoverflow.com/a/46848004/1316440). Certainement cela a été un oubli de ma part, la troisième section 'SELECT' de MariaDB renvoie le résultat attendu, voir [dbfiddle] (http://dbfiddle.uk/?rdbms=mariadb_10.2&fiddle=88d4e48eac1bee76e576ec883af781c9). Je vous remercie. – wchiquito

0

Il semble que vous ayez trouvé un bogue dans MySQL, signalez un bogue: bugs.mysql.com.

J'ai exécuté le script suivant dans MySQL et MariaDB (sans WITH PARSER ngram car actuellement en MariaDB il est pas pris en charge, voir Add "ngram" support to MariaDB) avec le résultat:

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)