Optimizing Gravity Forms with custom indexes
Gravity Forms saves every field value of every entry as its own row in the wp_gf_entry_meta table. On a form with thousands of entries, a View that filters or sorts by field values reads many of those rows, and an extra index can make it faster.
Warning: These changes are made directly in your database. Back up the database first, and try each change on a staging copy of your site before you make it on the live site. GravityKit is not responsible for problems that result from changes to your database.
The statements below use the wp_ table prefix. If your tables start with something else, such as wp7k_gf_entry_meta, use your own prefix in each statement. Your database tool lists the table names, so you can see the prefix there.
The indexes Gravity Forms creates #
Gravity Forms creates these indexes on its two entry tables. Keep all of them.
| Table | Index name | Columns |
|---|---|---|
wp_gf_entry_meta | PRIMARY | id |
meta_key | meta_key, first 191 characters | |
entry_id | entry_id | |
meta_value | meta_value, first 191 characters | |
wp_gf_entry | PRIMARY | id |
form_id | form_id | |
form_id_status | form_id, status |
If one of them goes missing, Gravity Forms adds it back the next time you update Gravity Forms. Until then, Gravity Forms’ System Status page says “There are issues with your database.” and marks the table “Table has not been upgraded successfully.” To add the index back straight away, go to Forms, then System Status, and click Re-run database upgrade.
Check which indexes a table has #
Open your database tool, such as phpMyAdmin in your hosting control panel, and run this statement in its SQL tab:
SHOW INDEX FROM wp_gf_entry_meta;For the entries table, run SHOW INDEX FROM wp_gf_entry; instead.
Each row of the result is one column of an index, so an index on three columns takes three rows. The Key_name column holds the index’s name and Seq_in_index the column’s place in it. A number in Sub_part, such as 191, means the index holds only that many characters of the column.
Compare each index with the table above: its columns, their order in Seq_in_index, and the Sub_part of each column. Two indexes with the same columns in the same order and the same Sub_part values hold the same data, whatever their names, so the second one only takes up space.
To see how much space the indexes take, run:
SHOW TABLE STATUS LIKE 'wp_gf_entry_meta';The result’s Data_length and Index_length columns are in bytes. The table’s rows are stored inside its PRIMARY index, so Data_length is the size of that index with the rows in it, and Index_length is the size of all the other indexes together. Every index you add makes Index_length grow.
Where other indexes come from
No GravityKit plugin creates indexes on wp_gf_entry or wp_gf_entry_meta. An index that isn’t in the table above was added by someone with access to the database or by another plugin. These names come from GravityKit:
| Index name | Table | Columns | Where it comes from |
|---|---|---|---|
idx_entry_meta_key_value | wp_gf_entry_meta | entry_id, meta_key, meta_value (191) | The statement in Add an index for field values below |
idx_entry_meta_value | wp_gf_entry_meta | entry_id, meta_key, meta_value (191) | Added by hand by GravityKit support while speeding up slow Views on large forms |
idx_meta_entry | wp_gf_entry_meta | meta_key, entry_id | Added by hand by GravityKit support, as above |
idx_form_status_created_by | wp_gf_entry | form_id, status, created_by, id | Added by hand by GravityKit support, as above |
The first two have the same definition, so a table needs only one of them. For any other name, the person or plugin that added it is the best source for what it was meant to speed up.
Measure a View’s query #
Measure the View before you add an index, so you have times to compare with afterwards.
- Install and activate the free Query Monitor plugin.
- While signed in as an administrator, open the page with your View and add
?nocache=1to the end of its address, or&nocache=1if the address already has a?. Without it, GravityView can show results it saved earlier, and the database isn’t asked at all. - In the admin bar, hover over Query Monitor’s figures and click Database Queries.
- Find the query that starts with
SELECT DISTINCTand includeswp_gf_entry. Query Monitor listsGF_Query->query()as its caller. Reload the page a few times and note the time Query Monitor shows for that query. - Copy the query, put
EXPLAINin front of it, and run it in your database tool.
The EXPLAIN result has one row for each table the query reads, and a table read twice gets two rows. The key column names the index the database chose for that row.
The table column shows the short name the query gives each table, such as t1 or m3. Look for that name in the query: wp_gf_entry AS t1 is the entries table, and wp_gf_entry_meta AS m3 ON (m3.entry_id = t1.id AND m3.meta_key = '5') reads the values of field 5.
Add an index for field values #
Gravity Forms finds a field’s value by the entry ID and the field ID, which wp_gf_entry_meta stores in entry_id and meta_key. This index starts with those two columns, followed by the first 191 characters of the value. If SHOW INDEX already lists an index on entry_id, meta_key and meta_value (191) in that order, such as idx_entry_meta_value, skip this step.
ALTER TABLE wp_gf_entry_meta
ADD INDEX idx_entry_meta_key_value (entry_id, meta_key, meta_value(191)),
ALGORITHM=INPLACE, LOCK=NONE;ALGORITHM=INPLACE, LOCK=NONE asks the database to build the index while the table stays open for reading and writing, so your forms keep taking entries. If the database can’t build it that way, the statement stops with an error and changes nothing.
The bigger the table, the longer the index takes to build. Run it at a quiet time, and time it on the staging copy first to see how long it takes on your data.
Then measure again: load the View with ?nocache=1 a few times, compare the query times with the ones you noted, and run EXPLAIN again. A row for wp_gf_entry_meta with idx_entry_meta_key_value in key uses the new index. The database chooses an index for each row from its own estimates, so one query can use the new index for one field and an older index for another.
If you get “Index column size too large”
The full error is Index column size too large. The maximum column size is 767 bytes. Whether this error can happen depends on the table’s row format. Check it:
SHOW TABLE STATUS LIKE 'wp_gf_entry_meta';Then look at the Row_format column:
| Row_format | What to do |
|---|---|
Compact or Redundant | These formats allow at most 767 bytes for an indexed column. Convert the table with the statement below, then run the index statement again. |
Dynamic or Compressed | These formats allow the index, so the limit comes from the database server’s settings. Send the full error and the Row_format value to your host. |
ALTER TABLE wp_gf_entry_meta ROW_FORMAT=DYNAMIC;Remove an index #
Gravity Forms and GravityKit plugins leave the choice of index to the database, so their queries work with or without an index you added. Removing one deletes no data, and every feature keeps working. Queries that used it can get slower.
An index belongs to one table, so name that table in the statement, along with the index’s name from the Key_name column. For an index on wp_gf_entry_meta:
ALTER TABLE wp_gf_entry_meta DROP INDEX idx_entry_meta_key_value;For an index on wp_gf_entry:
ALTER TABLE wp_gf_entry DROP INDEX idx_form_status_created_by;If the statement names the wrong table, the database answers with an error that starts with Can't DROP INDEX and changes nothing. Afterwards, run SHOW INDEX FROM on the same table to check that the index is gone. Leave the indexes Gravity Forms creates in place.