更新表中每一行的字段的最佳方法是什么?
我有一张名为艺术家的桌子。其中,有一个艺术家姓名字段 (artist_name)。然后有一个 SEO 友好的艺术家姓名字段,我们将其称为 search_name。
我的这张表中有超过 40,000 名艺术家。所以,我想将所有艺术家的名字转换为搜索友好的。实现这一目标的最佳方法是什么?这里不是寻找代码,只是想法。
这是我到目前为止所拥有的。我只是不确定是否应该致电所有 40,000 名艺术家,循环浏览并更新?
// 该艺术家姓名是否有任何符号、撇号等。如果有,请将其删除
// 该艺术家姓名是否有空格(披头士乐队)?如果是这样,请替换为 + (the+beatles)。
// 插入到搜索字段
I have a table called artists. Within it, there is a field for the artist name (artist_name). Then there is a field for SEO friendly artist name, we'll call it search_name.
I have over 40,000 artists in this table. So, I'd like to convert all artists names to search friendly. What is the best way to accomplish this? Not looking for code here, just ideas.
This is what I have thus far. I'm just not sure if I should call all 40,000 artists, loop through them and update?
// Does this artist name have any symbols, apostrophes, etc. If so, strip them out
// Does this artist have a space (the beatles)? If so, replace with + (the+beatles).
// insert into search field
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于 40,000 条记录并不算多,因此我会抓取所有记录并在内存中循环遍历它们。通过在内存中进行,唯一检查应该非常快。
最后,我只是将命令链接在一起,例如: $query .= "UPDATE arts SET search_name = $ generated_name[$i] WHERE id = $id[$i];"。
顺便说一句:我会用减号替换空格。
As 40,000 records aren't that much, I'd grab all of them and loop through them in memory. By doing it in memory, unique checks should be pretty fast.
In the end, I'd just chain the commands together like: $query .= "UPDATE artists SET search_name = $generated_name[$i] WHERE id = $id[$i];".
By the way: I'd replace spaces with a minus.
您可以仔细检查并创建一个两列宽的辅助表(id,安全),然后从那里插入它。
查询表 1
将艺术家姓名转换为安全名称
插入表 2
使用两个表的 id 进行匹配。这仅允许一对一匹配,但如果 id 是索引,如果您想要为单个艺术家提供多个安全名称,您可能需要创建第三列 (id | ArtistID | ArtistName)
You could go through and create a secondary table two columns wide (id, safe) and insert it from there.
Query Table 1
Convert artist names to safe names
Insert into Table 2
Use id of both tables to match them. This would only allow one to one matches though if id is the index, you may want to create a third column if you want multiple safe names for a single artist (id | artistID | artistName)
请考虑使用一些全文搜索引擎。例如,免费的 sphinx 搜索 - 它非常灵活、速度极快,并且支持词干提取。
Please consider using some full-text search engine. For example, the free sphinx search - it's quite flexible, extremely fast and it does support word stemming.