or ask around on community forum for advice. ','

## 3.8. SQL data sources (MySQL, PostgreSQL)

With all the SQL drivers, indexing generally works as follows.

- connection to the database is established;

- pre-query (see Section 12.1.11, “sql_query_pre”) is executed to perform any necessary initial setup, such as setting per-connection encoding with MySQL;

- main query (see Section 12.1.12, “sql_query”) is executed and the rows it returns are indexed;

- post-query (see Section 12.1.28, “sql_query_post”) is executed to perform any necessary cleanup;

- connection to the database is closed;

- indexer does the sorting phase (to be pedantic, index-type specific post-processing);

- connection to the database is established again;

- post-index query (see Section 12.1.29, “sql_query_post_index”) is executed to perform any necessary final cleanup;

- connection to the database is closed again.

Most options, such as database user/host/password, are straightforward. However, there are a few subtle things, which are discussed in more detail here.

### Ranged queries

Main query, which needs to fetch all the documents, can impose a read lock on the whole table and stall the concurrent queries (eg. INSERTs to MyISAM table), waste a lot of memory for result set, etc. To avoid this, Sphinx supports so-called ranged queries. With ranged queries, Sphinx first fetches min and max document IDs from the table, and then substitutes different ID intervals into main query text and runs the modified query to fetch another chunk of documents.