简体   繁体   English

优化在JOIN中使用REGEXP的SQL查询

[英]Optimize SQL-Query that is using REGEXP in a JOIN

I have the following situation: 我有以下情况:

Table Words: 表词:

| ID |   WORD |
|----|--------|
|  1 |     us |
|  2 |     to |
|  3 | belong |
|  4 |    are |
|  5 |   base |
|  6 |   your |
|  7 |    all |
|  8 |     is |
|  9 |  yours |

Table Sentence: 表语句:

| ID |                                  SENTENCE |
|----|-------------------------------------------|
|  1 | <<7>> <<6>> <<5>> <<4>> <<3>> <<2>> <<1>> |
|  2 |                         <<7>> <<8>> <<9>> |

And i want to replace the <<(\\d)>> with the equivalent word from the Word-Table. 我想用字表中的等效字替换<<(\\ d)>>。

So the result should be 所以结果应该是

| ID |                       SENTENCE |
|----|--------------------------------|
|  1 | all your base are belong to us |
|  2 |                   all is yours |

What i came up with is the following SQL-Code: 我想到的是以下SQL代码:

SELECT id, GROUP_CONCAT(word ORDER BY pos SEPARATOR ' ') AS sentence FROM (
    SELECT sentence.id, words.word, LOCATE(words.id, sentence.sentence) AS pos
    FROM sentence
    LEFT JOIN words
    ON (sentence.sentence REGEXP CONCAT('<<',words.id,'>>'))
    ) AS TEMP
GROUP BY id

I made a sqlfiddle for this: 我为此做了一个sqlfiddle:

http://sqlfiddle.com/#!2/634b8/4 http://sqlfiddle.com/#!2/634b8/4

The code basically is working, but i'd like to ask you pros if there is a way without a derived table or without filesort in the execution plan. 该代码基本上可以正常工作,但是我想问您专业人士,是否有一种在执行计划中没有派生表或没有文件排序的方法。

You should make a table with one entry per word, so your sentense (sic) can be made by joining on that table. 您应该制作一个表,每个单词有一个条目,因此可以通过加入该表来制作您的句子(原文如此)。 It would look something like this 看起来像这样

SentenceId, wordId, location
2,          7,       1
2,          8,       2
2,          9,       3

They way you have it set up, you are not taking advantage of your database, basically putting several points of data in 1 table-field. 他们以这种方式进行设置,而没有利用数据库,基本上是将多个数据点放在一个表字段中。

The location field (it is tempting to call it "order", but as this is an SQL keyword, don't do it, you'll hate yourself) can be used to 'sort' the sentence. 位置字段(很容易将其称为“ order”,但是由于这是一个SQL关键字,请不要这样做,您会讨厌自己)可用于对句子进行“排序”。

(and you might want to rename sentense to sentence?) (您可能想将SENSENSE重命名为句子?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM