Configuring the Database Server
OPUS runs against MySQL with nothing but a user allowed to create and drop databases and tables (Prerequisites), but a server left at MySQL’s defaults imports slowly and searches from disk. This chapter describes the settings that matter to OPUS, what each one does for it, and how to tell whether the indexes the database carries are the ones its traffic reads.
One server can import and serve at once, or the import can run on one server and the
database be copied to another that serves it, which the deploy chain’s database/
scripts do (Deployment and Operations).
Each setting below says which of those it is for. Settings go in the [mysqld]
section of the server’s option file. Those marked startup only take effect when the
server restarts; every other one can also be changed while it runs, with SET
PERSIST, which keeps the value across restarts.
Memory: the buffer pool
innodb_buffer_pool_size is the setting that decides how a search performs. InnoDB
reads every row and index page through the buffer pool, and a page that is not in it is
read from disk while the request waits. A search is a scan of the observation tables or
of their indexes, so a pool that holds them answers from memory and one that does not
answers at the speed of the disk.
Size it from the database you have, not from a rule of thumb. The size of each table of an imported database, data and indexes together, is:
SELECT table_name,
ROUND((data_length + index_length) / 1024 / 1024) AS mib
FROM information_schema.TABLES
WHERE table_schema = 'opus'
ORDER BY mib DESC;
with the database’s name in place of opus; ANALYZE TABLE on a table first brings
its figures up to date. The figures to read from it are these:
The total is the most a pool ever needs. A pool that size holds the whole database, and nothing a request reads ever comes from disk once it has been read once.
The total less
obs_filesandobs_previewis the least a pool should hold.obs_fileslists every file of every observation and is usually the largest table by far, andobs_previewholds the preview image descriptions, but both are read one observation, or one page of results, at a time, by primary key, so a request reads a few of their pages however large they are. The other observation tables are the ones a search scans.
Leave room beside the pool for the operating system, for MySQL’s per-connection memory, and, where they share the machine, for the web workers and memcached. On a server that imports, the pool also bounds the import’s index builds: finalization gives its concurrent index sorts a quarter of the pool between them (opus_import.steps.do_indexes), so a larger pool builds indexes with fewer merge passes. The database grows with every bundle the holdings gain, so run the query again after a full import, not only when the server is first set up.
The settings, and what each does for OPUS
Setting |
For |
What it does for OPUS |
|---|---|---|
|
both |
The memory InnoDB caches table and index pages in. Sized from the database, as above. |
|
import |
The size of the redo log. The default, 100 MiB, is small beside the gigabytes an
import writes, and when the log fills, the server holds writes back while it
flushes pages to make room; a larger log lets it absorb more of an import before
it must. A few gigabytes suit an import server. Before 8.0.30 the same is
|
|
both |
Startup only. |
|
import |
|
|
import |
|
|
import |
Startup only. |
|
import |
The threads the server sorts one index build with. Finalization builds the
indexes after the rows are loaded, several tables at once when |
|
import |
The sort memory of one index build. The import sets it for each of its own builds from the build’s estimated size and the buffer pool (above), so the server’s value matters only as the least each build gets. |
|
import |
How many pages a second InnoDB’s background flushing writes, normally and at most. The defaults, 200 and 2000, suit a spinning disk; an import writes faster than a server flushing at 200 pages a second keeps up with, and then waits for free pages. Set them from what the data directory’s device sustains, measured with a disk benchmark, not from the device’s rating. |
|
import |
The threads the server reads one clustered index with in parallel. For a query it
speeds only a |
|
serve |
Each web worker thread keeps its database connection open between requests
( |
|
serve |
Startup only. How much of each statement the server keeps when it counts statements by shape (Re-collecting the index statistics). At the default, 1024 bytes, OPUS’s wide search statements are cut off before the parts that tell them apart and are counted as one; 4096 keeps them distinct. The first is capped by the second, so raise both. |
|
serve |
Startup only. How many statement shapes the server counts. Once the table is full, every further shape is counted in one overflow row, so the counts describe the traffic only while that row holds few statements (Re-collecting the index statistics). |
|
neither |
These shape full-text indexes, and OPUS creates none: its autocomplete matches
with |
The optimizer’s statistics are the ones ANALYZE TABLE keeps for each index:
opus_import --analyze-tables, the last step of every finalization, refreshes them for
every observation and mult table.
Re-collecting the index statistics
Every index costs disk, buffer pool and import time, and earns its place only if a search reads it. The way to tell is the server’s own counters, read after it has served the site’s traffic for long enough to have seen the searches people make – a month of uptime on the database being judged. Two queries give what is needed.
How often each index was read, with its size:
SELECT u.OBJECT_NAME AS table_name,
u.INDEX_NAME AS index_name,
u.COUNT_READ AS reads,
ROUND(s.stat_value * @@innodb_page_size / 1024 / 1024) AS mib
FROM performance_schema.table_io_waits_summary_by_index_usage u
LEFT JOIN mysql.innodb_index_stats s
ON s.database_name = u.OBJECT_SCHEMA
AND s.table_name = u.OBJECT_NAME
AND s.index_name = u.INDEX_NAME
AND s.stat_name = 'size'
WHERE u.OBJECT_SCHEMA = 'opus'
ORDER BY u.OBJECT_NAME, u.COUNT_READ;
A row whose index_name is NULL counts the rows its table gave up to scans that
used no index at all, so a table with many such reads is searched by a column with no key.
Which statements took the time:
SELECT COUNT_STAR AS executions,
ROUND(SUM_TIMER_WAIT / 1e12, 1) AS total_s,
SUM_ROWS_EXAMINED AS rows_examined,
SUM_NO_INDEX_USED AS without_index,
DIGEST_TEXT
FROM performance_schema.events_statements_summary_by_digest
WHERE SCHEMA_NAME = 'opus'
ORDER BY SUM_TIMER_WAIT DESC
LIMIT 50;
How to read them:
The window is the server’s uptime. Both tables count from the moment the server started (
SHOW GLOBAL STATUS LIKE 'Uptime'gives it in seconds), and restarting it empties them. A database switched in during the window has been counted only since then.Keys only an import reads count as zero in a window with no import in it: the key on
obs_general’s bundle column, which the per-bundle delete reads, is one. An import on the same server during the window counts its own reads beside the site’s.The statement counts are complete only when the overflow row is small. Once the server has counted
performance_schema_digests_sizeshapes, it counts every further one in the row whoseDIGESTisNULL:SELECT COUNT_STAR FROM performance_schema.events_statements_summary_by_digest WHERE DIGEST IS NULL;
A count there that is large beside the other rows’ means the table filled early and the list leaves out much of the traffic; raise
performance_schema_digests_sizeand collect again.
What to do with them. The result is the input for removing plain keys – a primary or
unique key enforces a rule about the rows, and stays whatever its reads – by this rule:
a plain key goes when the window shows no read of it, or when it leads a composite key on
the same table (the composite answers every read the shorter key does), unless the
import, or a part of the application the traffic in the window did not exercise, reads by
it; a foreign key needs it; its column is one autocomplete searches, whose prefix match
reads it; or the other column of its 1/2 pair keeps one. A key that leads a
composite goes even then. Keys are declared in the
table schemas that ship with each release (Table Schemas), so removing
one is a change to a release, which takes effect at the next full import. A search whose
key was removed still answers, by a scan; the NULL rows above show it, and ALTER
TABLE ... ADD KEY on the served database restores the key until a release declares it
again.