简体   繁体   English

有关MySql索引和Asp.net C#的问题

[英]Question about MySql index and Asp.net C#

In Asp.net C# I use the following code to select from a MySql-database: 在Asp.net C#中,我使用以下代码从MySql数据库中进行选择:

string userid = "12";
MySqlCommand mysqlcmd = new MySqlCommand("SELECT productid FROM table WHERE userid=?userid", mysqlconn);
mysqlcmd.Parameters.AddWithValue("?userid", userid);
mysqlcmd.Connection.Open();
mysqlcmd.ExecuteReader();
...

In the table the userid is of the type int. 在表中,用户标识为int类型。 If I have an index in the table on userid what will happen with this query, will it be able to use the index in a good way even if the userid I add to the parameter is a string and the index in the table is int? 如果我在userid的表中有索引,此查询将如何处理,即使我添加到参数的userid是字符串并且表中的索引为int,它也能够很好地使用索引?

Would it give me better performance to before adding userid to the parameter convert userid to an int like this: 在向参数添加userid之前,将userid转换为int,是否会给我带来更好的性能:

int userid_int = Int32.Parse(userid);
MySqlParameter param = new MySqlParameter("?userid", MySqlDbType.Int32);
param.Value = userid_int;
cmd.Parameters.Add(param);

Or is it ok to only use: 还是可以只使用:

mysqlcmd.Parameters.AddWithValue("?userid", userid);

Indexes are used no matter what kind of datatype the indexed column is. 无论索引列是哪种数据类型,都使用索引。 The questions is, how efficient this will happen. 问题是,这将有多有效。

For instance, if you use string-columns with indexes for search you should ensure, that the values in that columns are differing from each other. 例如,如果您使用带有索引的字符串列进行搜索,则应确保该列中的值彼此不同。 If you have records like 如果您有类似的记录

index_column
------------
DHDHDSNDH233
DHDHDSNDH234
DHDHDSNDH235
DHDHDSNDH236

the index is a little bit useless because the engine has to load a lot of data for finding your keys. 该索引有点用处,因为引擎必须加载大量数据才能找到您的密钥。

This is the reason why a lot of people are using GUIDs when search-indexing a string-column. 这就是为什么许多人在对字符串列进行索引时使用GUID的原因。

If the column is'nt the PK, you have to live with that possible bottleneck. 如果该列不是PK,则必须忍受可能的瓶颈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM