Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop at first match in a sqlite query

Tags:

sql

sqlite

I have a sqlite3 database worth 4GB and 400k rows. The id column is a consecutive number starting at 1. If I query the following

select * from game where id = 1

After printing the first match the query continues until it reach the 400k row, thus taking a few seconds to finish the query.

How do I make the query stop at the first match?

Or how do I go directly to specific row since id and rowcount are the same?

like image 447
milarepa Avatar asked Jan 03 '15 05:01

milarepa


People also ask

What is wildcard in SQLite?

SQLite provides two wildcards for constructing patterns. They are percent sign % and underscore _ : The percent sign % wildcard matches any sequence of zero or more characters. The underscore _ wildcard matches any single character.

Does SQLite have a query optimizer?

The query optimizer in SQLite has basically two choices on how to implement this query. (There are actually six different choices, but we will only consider two of them here.) Pseudocode below demonstrating these two choices. The same indexes are used to speed up every loop in both implementation options.

How do I order ascending in SQLite?

It allows you to sort the result set based on one or more columns in ascending or descending order. In this syntax, you place the column name by which you want to sort after the ORDER BY clause followed by the ASC or DESC keyword. The ASC keyword means ascending. And the DESC keyword means descending.

Does SQLite have except?

Description. The SQLite EXCEPT operator is used to return all rows in the first SELECT statement that are not returned by the second SELECT statement. Each SELECT statement will define a dataset.


1 Answers

Just add a LIMIT 1 to your query:

SELECT * FROM game WHERE id = 1 LIMIT 1;
like image 93
ianaya89 Avatar answered Sep 22 '22 20:09

ianaya89