c# - 使用二进制搜索或并行编程在 C# 中搜索 SQL Server 数据库

标签 c# asp.net sql-server database

我有一个 SQL Server 数据库,表中有非常大的记录。我正在尝试使用 C# 构建一个 ASP.NET 网站以在我的 SQL Server 数据库中进行搜索,结果将显示在 gridview 中。数据库在 NIM 列中已有主键。 这是我当前的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class SearchPage : System.Web.UI.Page
{
    SqlConnection cnn = new SqlConnection("Data Source=GILANG-PC\\SQLEXPRESS;Initial Catalog=Mahasiswa;Integrated Security=True");
    protected void Page_Load(object sender, EventArgs e)
    {

    }
protected void Button1_Click(object sender, EventArgs e)
{
    String str = "select * from Mahasiswa where (NIM like '%' + @search + '%')";
    SqlCommand sqlcmd = new SqlCommand(str, cnn);
    sqlcmd.Parameters.Add("@search", SqlDbType.NVarChar).Value = TextBox1.Text;

    cnn.Open();
    sqlcmd.ExecuteNonQuery();
    SqlDataAdapter da = new SqlDataAdapter();
    da.SelectCommand = sqlcmd;
    DataSet ds = new DataSet();
    da.Fill(ds, "NIM");
    GridView1.DataSource = ds;
    GridView1.DataBind();
    cnn.Close();
}
}

上面的代码可以正常运行。但是我对两件事很好奇,如何使用二进制搜索方法来搜索该数据库?是否可以使用并行代码来搜索该数据库? 我已经做了一些研究和实验,但没有用。或许这里的高手可以帮忙。谢谢...

最佳答案

An index can be created in a table to find data more quickly and efficiently.

The users cannot see the indexes, they are just used to speed up searches/queries.

Note: Updating a table with indexes takes more time than updating a table without (because the indexes also need an update). So you should only create indexes on columns (and tables) that will be frequently searched against.

这是一个 Article .你可以在网上搜索很多关于 SQL 索引的文章。

如果您需要有关 SQL 索引及其工作原理的文章 see this !

What kind of data structure is an index?

B- trees are the most commonly used data structures for indexes. The reason B- trees are the most popular data structure for indexes is due to the fact that they are time efficient – because look-ups, deletions, and insertions can all be done in logarithmic time. And, another major reason B- trees are more commonly used is because the data that is stored inside the B- tree can be sorted. The RDBMS typically determines which data structure is actually used for an index. But, in some scenarios with certain RDBMS’s, you can actually specify which data structure you want your database to use when you create the index itself.

正如您在此处的文章中所见,我们的复杂度与二进制搜索 -> 对数时间相同。

编辑:就像 learningNew 所说,如果您使用 LIKE,索引将无济于事,因此您需要将 LIKE 更改为 SQL Contains 和 CREATE FULLTEXT INDEX对于此专栏。

关于c# - 使用二进制搜索或并行编程在 C# 中搜索 SQL Server 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32087566/

相关文章:

ASP.NET SQL Server session 状态非常慢

SQL Server : how to delete based on another table

c# - 阻塞线程是否会导致性能损失?

c# - 如何编写将分隔字符串转换为列表的通用扩展方法?

asp.net - 单元测试 - 用户帐户

asp.net - 如何在 ASP.NET 中长时间操作期间显示 "please wait"消息?

sql-server - 从 Powershell : null values inserted as "blank" 插入 SQL

c# - 在 Linq to NHibernate 中测试条件的最佳方法是什么?

c# - C# Lambda 表达式类型安全吗?何时(编译时/运行时)检查它们?

javascript - 获取一个 asp radiobuttonlist 通过 jquery 触发它的 TextChanged 事件?