gpt4 book ai didi

php - 搜索词结构的最佳方式?

转载 作者:行者123 更新时间:2023-11-29 04:08:35 24 4
gpt4 key购买 nike

我想建立一个数据库,其中大约有 400 个字符串。我想让数据库可搜索。

数据库的结构如下:

Brand | model |additional products | price | search words | (this is 1 string, there will about 400 strings)

每个字符串将包含 2 到 50 个搜索词。搜索是通过单击复选框完成的,标记的复选框单词将在数据库中搜索。

我的问题是索引所有搜索词的最佳方式是什么?我在想两种方法:

  1. search words 字段中,所有可搜索的词将显示为:1GB RAM、512GB RAM、ATA、SATA ... 等等每个字符串.这意味着所有单词在以“,”分隔的特定字符串上都将处于相同的原始状态。

  2. 每个搜索词都有自己的行,例如:|搜索词 1|搜索词 2|搜索词 3 |搜索 4 个词 5|..... 等等。在 |search words 1| 中,单词 1GB RAM 将是。在 |搜索词 2|512GB RAM 将以此类推……这意味着在字符串中,可能有一半的搜索词行将填充搜索词。

    <

在选项 2 中,数据库中将有超过 50 行,所有搜索词都在不同的列中(每个产品的每列中有 1 个)。在选项 1 中,每个产品将有 1 行,所有单词都在同一列中。

或者有更好的方法吗?

最佳答案

尽管另一个答案被接受了......我对这个想法做了更多的解释,因为我觉得它符合“最佳实践”并且允许您将多个单词与一个项目相关联而不重复数据。

您最终应该得到三个表:

item:        item_id | Brand | model |additional products | price 

word: word_id | word

item_word: item_word_id | item_id | word_id

数据看起来像:

项目:

item_id   brand     model        additional_products   price 
1 nokia g5 100
2 toshiba satellite 1000

单词:

word_id   word 
1 1 GB
2 ATA
3 SATA
4 512BG RAM

item_word:

item_word_id    itwm_id     word_id
1 1 1
2 1 2
3 2 3
4 2 4

因此 nokia 有这些词:1 GB,ATAtoshiba 有这些词:SATA,512BG RAM。 (我意识到这没有多大意义,这只是一个例子)


然后像这样查询它..

select  item.*, word 

from item

join item_word on item.item_id = item_word.item_id

join word on item_word.word_id = word.word_id

然后像这样过滤它

select  item.*, word 

from item

join item_word on item.item_id = item_word.item_id

join word on item_word.word_id = word.word_id

where word in ('1GB RAM', '512GB RAM', 'ATA')

看看什么是最相关的结果,您甚至可以尝试...

select  item.item_id, item.brand, item.model, count(*) as word_count

from item

join item_word on item.item_id = item_word.item_id

join word on item_word.word_id = word.word_id

where word in ('1GB RAM', '512GB RAM', 'ATA')

group by item.item_id, item.brand, item.model

order by count(*) desc

对于匹配所有提供的单词的东西,你会使用...

select  item.item_id, item.brand, item.model, count(*) as word_count

from item

join item_word on item.item_id = item_word.item_id

join word on item_word.word_id = word.word_id

where word in ('1GB RAM', 'ATA')

group by item.item_id, item.brand, item.model

having count(*)=2

其中 3 是您的 in 语句中的单词数... word in ('1GB RAM', 'ATA')。在本例中是 2


如果你只是...

item:  Brand | model |additional products | price | long_word_string

然后你必须做...

 select  * 
from item
where long_word_string like '1GB RAM' or word like 'ATA'

甚至...

 select  * 
from item
where long_word_string regexp '1GB RAM|ATA'

但这些都是非常低效/昂贵的方法......最好只是规范化一些东西,这样你就不会存储额外的数据并在试图将其取出时降低性能

这有意义吗?它能回答您的问题吗?

编辑:我的回答只剩下两张表了……我现在担心 OP 的数据库。

关于php - 搜索词结构的最佳方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19820500/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com