如何从数据库中查询 1 和 0 字符的数组?

发布于 2024-07-14 00:45:20 字数 106 浏览 4 评论 0原文

假设您有一长串字符,要么是 1,要么是 0,有点像位向量,但位于数据库列上。 您如何查询以了解设置/未设置哪些值? 假设您需要知道 char 500 和 char 1500 是否为“true”。

Say you had a long array of chars that are either 1 or 0, kind of like a bitvector, but on a database column. How would you query to know what values are set/no set? Say you need to know if the char 500 and char 1500 are "true" or not.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

往日 2024-07-21 00:45:21
SELECT
  Id
FROM
  BitVectorTable
WHERE
  SUBSTRING(BitVector, 500, 1) = '1'
  AND SUBSTRING(BitVector, 1000, 1) = '1'

但是,没有索引可以用于此类查询。 当你有很多行时,这会很快变得很慢。

编辑:至少在 SQL Server 上,所有内置字符串函数都是确定性。 这意味着您可以研究基于整个组合值的 SUBSTRING() 结果创建计算列的可能性,为每个索引添加索引。 插入会变慢,表大小会增加,但搜索会非常快。

SELECT
  Id
FROM
  BitVectorTable
WHERE
  BitVector_0500 = '1'
  AND BitVector_1000 = '1'

编辑#2:SQL Server 的限制是:

  • 每个 1,024 列普通表
  • 每个“宽”表 30.000 列
SELECT
  Id
FROM
  BitVectorTable
WHERE
  SUBSTRING(BitVector, 500, 1) = '1'
  AND SUBSTRING(BitVector, 1000, 1) = '1'

No index can be used for this kind of query, though. When you have many rows, this will get slow very quickly.

Edit: On SQL Server at least, all built-in string functions are deterministic. That means you could look into the possibility to make computed columns based on the SUBSTRING() results for the whole combined value, putting an index on each of them. Inserts will be slower, table size will increase, but searches will be really fast.

SELECT
  Id
FROM
  BitVectorTable
WHERE
  BitVector_0500 = '1'
  AND BitVector_1000 = '1'

Edit #2: The limits for SQL Server are:

  • 1,024 columns per normal table
  • 30.000 columns per "wide" table
转身以后 2024-07-21 00:45:21

在 MySQL 中,使用 substring

select foo from bar 
where substring(col, 500,1)='1' and substring(col, 1500,1)='1';

这样但效率会相当低,您可能需要重新考虑您的架构。 例如,您可以单独存储每个位,以权衡空间和速度...

create table foo
(
   id int not null,
   bar varchar(128),
   primary key(id)
);

create table foobit
(
   int foo_id int not null,
   int idx int not null,
   value tinyint not null,

   primary key(foo_id,idx),
   index(idx,value)
);

这将被查询

   select foo.bar from foo
   inner join foobit as bit500
      on(foo.id=bit500.foo_id and bit500.idx=500)
   inner join foobit as bit1500
      on(foo.id=bit1500.foo_id and bit1500.idx=1500)
   where
      bit500.value=1 and bit1500.value=1;

显然会消耗更多存储空间,但对于这些查询操作来说应该更快,因为将使用索引。

In MySQL, something using substring like

select foo from bar 
where substring(col, 500,1)='1' and substring(col, 1500,1)='1';

This will be pretty inefficient though, you might want to rethink your schema. For example, you could store each bit separately to tradeoff space for speed...

create table foo
(
   id int not null,
   bar varchar(128),
   primary key(id)
);

create table foobit
(
   int foo_id int not null,
   int idx int not null,
   value tinyint not null,

   primary key(foo_id,idx),
   index(idx,value)
);

Which would be queried

   select foo.bar from foo
   inner join foobit as bit500
      on(foo.id=bit500.foo_id and bit500.idx=500)
   inner join foobit as bit1500
      on(foo.id=bit1500.foo_id and bit1500.idx=1500)
   where
      bit500.value=1 and bit1500.value=1;

Obviously consumes more storage, but should be faster for those query operations as an index will be used.

甜心 2024-07-21 00:45:21

我会将列转换为多个位列并重写相关代码 - 位掩码比字符串比较快得多。 但如果您不能这样做,则必须使用特定于数据库的函数。 正则表达式可能是一种选择

-- Flavor: MySql
SELECT * FROM table WHERE column REGEXP "^.{499}1.{999}1"

I would convert the column to multiple bit-columns and rewrite the relevant code - Bit masks are so much faster than string comparisons. But if you can't do that, you must use db-specific functions. Regular expressions could be an option

-- Flavor: MySql
SELECT * FROM table WHERE column REGEXP "^.{499}1.{999}1"
三人与歌 2024-07-21 00:45:21
select substring(your_col, 500,1) as char500,
substring(your_col, 1500,1) as char1500 from your_table;
select substring(your_col, 500,1) as char500,
substring(your_col, 1500,1) as char1500 from your_table;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文