Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-completion for names against a SQL database

I have a text field in my web app where I want to do auto-completion (e.g. the user types "St" and I can suggest "Steve"). The names I'm matching against are in a SQL database table of users. My question is, how can I make this happen in a way that will scale to massive amounts of users?

  1. There's DB full text search or something like Lucene. Would that even be appropriate for a "starts with" query like this?

  2. Is there a way to set up a normal DB index for "starts with" type searches?

  3. Any other ideas that I'm totally missing?

Any help would be appreciated. Thanks.

like image 933
Sean Avatar asked Sep 30 '09 17:09

Sean


People also ask

How do you autocomplete in SQL?

Go to Tools -> Options -> Text Editor -> Transact-SQL -> IntelliSense -> Enable IntelliSense, as shown in the snippet below. Open New Query Window -> Go to Edit -> Expand IntelliSense -> Click Refresh Local Cache or press the shortcut key (CTRL + SHIFT + R) to refresh the local cache as shown in the snippet below.

Can you automate a SQL query?

Automating the executing of SQL queries can be handled by any client tool which can be scheduled. Typical solutions are in place by the database vendor already. Like Microsoft SQL server has the SQL Server Agent and MySQL has the MySQL Event Scheduler.

Which tool allows you to create SQL queries to be executed against a database?

Microsoft SQL Server Management Studio allows users to create and edit SQL queries and manage databases. Microsoft SQL Server Management Studio has been on the market for a long time.

How do I enable auto suggestions in SQL Server Management Studio?

Open SSMS, click Tools -> Options -> Expand Text Editor -> Expand Transact-SQL and click on IntelliSense as shown in the snippet below. Under Transact-SQL IntelliSense Settings ensure “Enable IntelliSense” checkbox is enabled.


2 Answers

These should do the job as long as you have an index on the name column.

SQL Server:

SELECT TOP 10 name FROM names WHERE name LIKE 'St%'

MySQL (according to Bart J):

SELECT name FROM names WHERE name LIKE 'St%' LIMIT 10

Oracle:

SELECT name FROM names WHERE name LIKE 'St%' AND rownum < 10
like image 133
erikkallen Avatar answered Sep 28 '22 23:09

erikkallen


If you have an ordered index on the field you want to autocomplete then it can be used in a "starts with" style query.

like image 32
tster Avatar answered Sep 28 '22 22:09

tster