简体   繁体   English

如何使用索引使Web应用程序中的数据库访问更快?

[英]How to use indexing to make database access faster in web applications?

In a web based java application , I have a table with this schema 在基于Web的Java应用程序中 ,我有一个具有此架构的表

 Column          Datatype

 Userid          char(25) primary key;
 name            char(100)
 address         varchar(120)
 email           char(50) UNIQUE;

with 120000 records. 120000条记录。 (Cardinality) (基数)

Now, I want to fetch the matching names (character by character) stored under column -> name. 现在,我想获取存储在列->名称下的匹配名称(按字符)。 when I start typing in a search box (through ajax obviously). 当我开始在搜索框中输入内容时(显然是通过Ajax)。

eg if I write p then all names starting from letter p will be retrieved and would come in the list to populate. 例如,如果我写了p,则将从字母p开头的所有名称都将被检索并出现在列表中以进行填充。

Right now I am not using any indexing . 现在我没有使用任何索引。 So how could I use it to make the access/search faster . 因此,我如何使用它来加快访问/搜索的速度

The database I am using is MySql 5 and java as frontend . 我正在使用的数据库MySql 5java作为前端

Any suggestion for using appropriate collection for this purpose or any hashing would help in the database if yes please elaborate. 任何建议使用适当的集合用于此目的或任何哈希将有助于数据库,如果是,请详细说明。

Though I have got the right track for this StackOverflowers. 尽管我对此StackOverflowers有正确的了解。 If I modify the question and use hashing in place of indexing with all its definition and usage statistics then how to do that and is it beneficial? 如果我修改问题并使用散列代替索引及其所有定义和使用情况统计信息,那么该怎么做,这是否有益?

The simplest way is just: 最简单的方法是:

ALTER TABLE `your_table` ADD INDEX(`name`);

And also, as names are variable length, use VARCHAR instead. 而且,由于名称是可变长度的,因此请改用VARCHAR。 For your key, use INT! 对于您的密钥,请使用INT!

You can shrink your index to one byte if you search by one character only: 如果仅搜索一个字符,则可以将索引缩小到一个字节:

ALTER TABLE `your_table` ADD INDEX `name`(`name`(1));

Then, for better performance, you can use covering index. 然后,为了获得更好的性能,可以使用覆盖索引。 It means you need to have all required data indexed properly so it resides in memory (make sure you configured your server properly) and MySQL does not need to read from disk. 这意味着您需要正确索引所有必需的数据,以便它们驻留在内存中(确保正确配置了服务器),并且MySQL不需要从磁盘读取数据。

By default, PRIMARY KEY and UNIQUE properties are indexed in the table. 默认情况下,表中索引了PRIMARY KEYUNIQUE属性。 So, your email and userid are already indexed. 因此,您的emailuserid已被索引。 ( NOTE: I seriously suggest you not to use CHAR for userid ) 注意:我强烈建议您不要将CHAR用作userid

And use 并使用

INDEX `<any-name-here>` (`name`) # The `name` is the column and `<any-
                                 # name-here>` is the name of index.
CREATE INDEX id_index ON employee_table (employeeID);

Create the indexing on the columns you will have in WHERE clause. 在WHERE子句中的列上创建索引。

If your query will be most likely on name, create indexing on name column. 如果您的查询最有可能使用名称,请在“名称”列上创建索引。

Remember this will leverage your insert/update record to that table which means your INSERT/UPDATE operations will be executed slower. 请记住,这会将您的插入/更新记录用于该表,这意味着您的INSERT / UPDATE操作将执行得较慢。

I wouldn't use DB queries. 我不会使用数据库查询。 You could preload the (unique) names in a String array and perform a binary search for the characters typed so far. 您可以将(唯一)名称预加载到String数组中,然后对到目前为止键入的字符执行二进制搜索。 This ensures maximum responisveness. 这样可以确保最大的响应度。

In generell you have to understand how a database index works. 在Generell中,您必须了解数据库索引的工作方式。

But for your problems, some suggestions: 但是对于您的问题,一些建议:

If you are using something like this (sorry, I don't know the MySql wildcard character, assuming it is %) 如果您使用的是这样的东西(对不起,假设它是%,我不知道MySql通配符)

select * from MyTable where name like '%a%'

where a is a letter the user entered - this will never work on bigger tables, because the first % says that there are any characters before a - this means the whole table has to be scanned (this means the index is nearly useless). 其中a是用户输入的字母-永远不会在较大的表上使用,因为第一个%表示a之前有任何字符-这意味着必须扫描整个表(这意味着索引几乎没有用)。

Consider implementing the Autocompleter so that it starts autocompleting after at least 3 characters, which would lead to SQL similar to this: 考虑实现自动完成程序,以便它在至少3个字符之后开始自动完成,这将导致类似于以下内容的SQL:

select * from MyTable where name like 'abc%'

This would allow the database to use the index, because it only has use the data beginning with "abc" 这将允许数据库使用索引,因为它仅使用以“ abc”开头的数据

Unfortunately you do not have an index at all on the column which can be used for your query, so you can add it via the following command: 不幸的是,该列上根本没有可用于查询的索引,因此可以通过以下命令将其添加:

ALTER TABLE 'table' ADD INDEX('column_you_search')

Hope this helps. 希望这可以帮助。

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

相关问题 如何使数据库中的数据可从其他Web应用程序访问 - How to make data from a database accessible from other web applications Web应用程序的并发数据库访问模式 - Concurrent database access pattern for web applications 如何使用 Spring Data Jpa 和 postgres 中的索引使我的本机查询更快 - How to make my native query faster using indexing in Spring Data Jpa and postgres Web应用程序MongoDB数据库结构 - Web applications MongoDB database structure 如何使我的 function 用于我的数据库的测试数据更快? - How do I make my function for testdata for my database faster? 如何与android应用程序和外部数据库建立连接? - How can i make connection with android applications and external database? 如何在其他应用程序(例如Web)中使用Android R.java? - How to use Android R.java in other applications (web, for example)? 如何使用jax-rs和mongo在Java Web应用程序中更快地访问数据库 - how to make database access quicker in a java web app with jax-rs and mongo 为时间要求严格的Web应用程序组合java spring / thread和数据库访问 - Combining java spring/thread and database access for time-critical web applications 如何使用多线程使我的应用程序更快 - How to use multi-threading to make my application faster
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM