mysql sphinx生成唯一id
我将我的数据库分为 8 个部分,其中每个部分都包含带有表 user_data 的数据库,为了获得更好的搜索性能,我使用 sphinx 来索引所有这些数据,但我遇到了一个问题...因为表 user_data 没有任何唯一字段来表示每个部分行,但它是一个一对多的表,我无法正确运行我的 sphinx 索引,因为它需要唯一的 id,并且这种方式可以解决重复的 id ...知道我该如何解决这个问题吗?或者通过来自不同段的所有子索引生成唯一的ID?
示例:
SELECT user_id, item_id, info
FROM user_data
返回类似以下内容:
+----------+-----------------------+
| user_id | item_id | info |
+----------+-----------------------+
| 10 | 151 | asdf |
| 10 | 152 | test |
| 11 | 151 | 545 |
| 12 | 151 | sdfsd |
| 12 | 152 | eewwe |
| 12 | 153 | dfsd |
但我必须知道
+----------+-----------------------------+
| user_id | item_id | info | id |
+----------+-----------------------------+
| 10 | 151 | asdf | 1 |
| 10 | 152 | test | 2 |
| 11 | 151 | 545 | 3 |
| 12 | 151 | sdfsd | 4 |
| 12 | 152 | eewwe | 5 |
| 12 | 153 | dfsd | 6 |
id 当然在所有段中必须是唯一的
I have my database segmented in 8 parts, where each part contains database with table user_data, for better search performance im using sphinx to index all those data but im come accross one problem ... since table user_data dont have any unique field to represent each row but its a 1 to many table i have trouble to run my sphinx index correctly since it requires unique id and this way resolve in duplicate ids ... any idea how can i workaround this? or generate unique id throught all sub indexes from different segments?
example:
SELECT user_id, item_id, info
FROM user_data
Which returns something like:
+----------+-----------------------+
| user_id | item_id | info |
+----------+-----------------------+
| 10 | 151 | asdf |
| 10 | 152 | test |
| 11 | 151 | 545 |
| 12 | 151 | sdfsd |
| 12 | 152 | eewwe |
| 12 | 153 | dfsd |
but i have to get
+----------+-----------------------------+
| user_id | item_id | info | id |
+----------+-----------------------------+
| 10 | 151 | asdf | 1 |
| 10 | 152 | test | 2 |
| 11 | 151 | 545 | 3 |
| 12 | 151 | sdfsd | 4 |
| 12 | 152 | eewwe | 5 |
| 12 | 153 | dfsd | 6 |
of course id must be unique throght all segments
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先,您应该在操作查询之前设置来设置变量,
然后使用此变量来获取虚构的自动增量
first of all you should set before action query to set your variable
then use this variable to get fictional auto increment
我不熟悉 Sphinx,但如果您希望创建跨表唯一的 id,在您的情况下:
id
字段设置为AUTO_INCRMENT
并将其起始编号设置为特定范围的开始。I'm unfamiliar with Sphinx, but if you're looking to create ids that are unique across tables, in your case:
id
fields to beAUTO_INCREMENT
and set their start numbers at the beginning of the particular range.您可以在索引时执行类似的操作:
SELECT user_id + 10 * 1 AS id, 1 ASegment_id, itme_id, info FROM user_data_1
...添加
segment_id
。您将拥有其中 8 个,因此索引查询将类似于:然后,当您查询 sphinx 并返回 ID 时,只需将
id
除以segment_id
并减去10
。这样所有的 id 在 sphinx 中都是唯一的。只需确保属性类型可以处理您要索引的 id 的大小即可。You could do something like this while indexing:
SELECT user_id + 10 * 1 AS id, 1 AS segment_id, itme_id, info FROM user_data_1
... adding a
segment_id
. You would have eight of these, so the indexing query would look something like:Then when you query sphinx and get back the IDs, just undo the arithmetic by dividing the
id
bysegment_id
and subtracting10
. This way all the ids will be unique within sphinx. Just make sure the attribute type can handle the size of the ids you'll be indexing.正如另一个答案中建议的那样,使用 UUID。但sphinx不能使用UUID作为id。你需要一个 INT。因此使用UUID_SHORT,然后你就有一个唯一的整数(对于mysql)。如果这不能开箱即用(例如,如果您正在使用 Ubuntu-11.04),您将收到如下错误:
您将需要使用 –enable-id64 编译 sphinx 源代码,或者只需访问 sphinx 网站并获取最新的软件包(使用 –enable-id64 编译)。 此博客条目
As proposed in another answer saying to use UUID. But sphinx can not use UUID as id. You will need an INT. Therefor use UUID_SHORT and then you have a unique integer (for mysql). If this does not work out of the box (e.g. if you aare using Ubuntu-11.04), you will get an error like this:
You will need to compile sphinx source with –enable-id64, or just go to sphinx website and get an up to date package (which is compiled with –enable-id64). A more complete example of this indexing method is given in this blog entry
我们使用 crc32(uuid_short()) 来实现 sphinx 的 32 位。这在大多数情况下都有效!办公室。不能依赖 32 位摘要
We are using crc32(uuid_short()) for 32 bit implementations of sphinx. This works, most of the time! Ofc. one cannot rely upon a 32 bit digest of a