-6

我的表中有 400 万行。(大小约为 300 GB),我想从 sql server 数据库中读取表中的所有行。我在 C# 中使用了以下代码。这需要时间。请建议我一些改进。

            List<int> hit_block_index = new List<int>();

            /* Here i process some other and populate hit_block_index with integers */

            string _query = "SELECT TraceName,BlockVector FROM Trace";

            SqlConnection _connection = new SqlConnection(connection_string);

            _connection.Open();

            SqlCommand _command = new SqlCommand(_query, _connection);

            SqlDataReader data_reader = _command.ExecuteReader();

            Byte[] block_vector=null;

            string trace_name = null;

            BitArray trace = null;

            int max_coverage = 0;

            while (data_reader.Read())
            {
                  int coverage = 0;

                  block_vector = (byte[])data_reader["BlockVector"];

                  trace_name = (string)data_reader["TraceName"];

                  BitArray trace = new BitArray(block_vector);

                  foreach (int x in hit_blocks_index)
                  {
                       if (trace[x])
                       {
                           coverage++;
                       }
                  }

                  Console.WriteLine("hit count is:" + coverage);

                  if (coverage > max_coverage)
                  {
                         most_covered_trace = trace_name;
                         most_covered_array = trace;
                         max_coverage = coverage;
                  }
           }
4

2 回答 2

1

这样的事情可能会奏效。我还不确定效率 - 它可能取决于您正在寻找的点击量:

create type HitBlocks as table (
    HitIndex int not null
)
go
create procedure FindMaxCover
    @Hits HitBlocks readonly
as
    ;With DecomposedBlocks as (
        select (HitIndex/8)+1 as ByteIndex,POWER(2,(HitIndex%8)) as BitMask
        from @Hits
    ), Coverage as (
        select
            t.TraceName,SUM(CASE WHEN SUBSTRING(t.BlockVector,db.ByteIndex,1) & BitMask != 0 THEN 1 ELSE 0 END) as Coverage
        from
            Trace t
                cross join
            DecomposedBlocks db
        group by
            t.TraceName
    ), Ranked as (
        select *,RANK() OVER (ORDER BY Coverage desc) as rk
        from Coverage
    )
    select
        t.TraceName,
        t.BlockVector,
        r.Coverage
    from
        Ranked r
            inner join
        Trace t
            on
                r.TraceName = t.TraceName
    where rk = 1

目前,如果有多个具有相同覆盖级别的结果,这将返回多行。您可能还需要调整 a) 我的期望和您的期望之间的一些错误,以及 b) 在计算正确BitMask值时可能存在一些字节顺序问题。

DataTable从您的代码中,您将使用当前存储的值填充 ahit_block_index并将其作为@Hits 参数传递。

于 2013-08-01T07:45:01.453 回答
1

如果您真的必须读取所有数据......通过 StoredProcedure 或您的引擎允许的任何方式将您的代码放入数据库中。完全传输数据库是没有意义的。

除此之外,您真的应该考虑选择另一种策略。示例 1:您可以在插入时创建触发器。在插入一个值时,您可以在不读取所有数据的情况下重新计算覆盖率(如果可能) 示例 2:您可以使用 SQL Azure 联合或 Azure Worker Role - 来扩展您的问题。

于 2013-08-01T12:30:40.437 回答