You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Enhance SQL performance guidance in migration script
- Added detailed recommendations for using primary and foreign keys, including examples for product and sales tables.
- Introduced best practices for indexing, query performance debugging, and database maintenance commands.
- Included advice on lazy loading and reducing the number of queries to optimize SQLPage applications.
Always use foreign keys instead of trying to store redundant data such as store names in the sales table.
40
63
41
64
This way, when you need to display the list of stores in your application, you don''t have to
42
65
run a slow `select distinct store from sales`, that would have to go through your millions of sales
43
66
(*even if you have an index on the store column*), you just query the tiny `stores` table directly.
44
67
68
+
You also need to use the right [data types](https://en.wikipedia.org/wiki/Data_type) for your columns,
69
+
otherwise you will waste a lot of space and time converting data at query time.
70
+
See [postgreSQL data types](https://www.postgresql.org/docs/current/datatype.html),
71
+
[MySQL data types](https://dev.mysql.com/doc/refman/8.0/en/data-types.html),
72
+
[Microsoft SQL Server data types](https://learn.microsoft.com/en-us/sql/t-sql/data-types/data-types-transact-sql?view=sql-server-ver16),
73
+
[SQLite data types](https://www.sqlite.org/datatype3.html).
74
+
45
75
[Denormalization](https://en.wikipedia.org/wiki/Denormalization) can be introduced
46
76
only after you have already normalized your data, and is often not required at all.
47
77
@@ -89,8 +119,56 @@ group by store_name;
89
119
90
120
### Use database indices
91
121
122
+
When a query on a large table uses non-primary column in a `WHERE`, `GROUP BY`, `ORDER BY`, or `JOIN`,
123
+
you should create an [index](https://en.wikipedia.org/wiki/Database_index) on that column.
124
+
When multiple columns are used in the query, you should create a composite index on those columns.
125
+
When creating a composite index, the order of the columns is important.
126
+
The most frequently used columns should be first.
127
+
128
+
```sql
129
+
create index idx_sales_store_date on sale (store_id, sale_date); -- useful for queries that filter by "store" or by "store and date"
130
+
create index idx_sales_product_date on sale (product_id, sale_date);
131
+
create index idx_sales_store_product_date on sale (store_id, product_id, sale_date);
132
+
```
133
+
134
+
Indexes are updated automatically when the table is modified.
135
+
They slow down the insertion and deletion of rows in the table,
136
+
but speed up the retrieval of rows in queries that use the indexed columns.
137
+
92
138
### Query performance debugging
93
139
140
+
When a query is slow, you can use the `EXPLAIN` keyword to see how the database will execute the query.
141
+
Just add `EXPLAIN` before the query you want to analyze.
142
+
143
+
On PostgreSQL, you can use a tool like [explain.dalibo.com](https://explain.dalibo.com/) to visualize the query plan.
144
+
145
+
What to look for:
146
+
- Are indexes used? You should see references to the indices you created.
147
+
- Are full table scans used? Large tables should never be scanned.
148
+
- Are expensive operations used? Such as sorting, hashing, bitmap index scans, etc.
149
+
- Are operations happening in the order you expected them to? Filtering large tables should come first.
150
+
151
+
### Vacuum your database regularly
152
+
153
+
On PostgreSQL, you can use the [`VACUUM`](https://www.postgresql.org/docs/current/sql-vacuum.html) command to garbage-collect and analyze a database.
154
+
155
+
On MySQL, you can use the [`OPTIMIZE TABLE`](https://dev.mysql.com/doc/refman/8.0/en/optimize-table.html) command to reorganize it on disk and make it faster.
156
+
On Microsoft SQL Server, you can use the [`DBCC DBREINDEX`](https://learn.microsoft.com/en-us/sql/t-sql/database-console-commands/dbcc-dbreindex-transact-sql?view=sql-server-ver17) command to rebuild the indexes.
157
+
On SQLite, you can use the [`VACUUM`](https://www.sqlite.org/lang_vacuum.html) command to garbage-collect and analyze the database.
158
+
159
+
### Use the right database engine
160
+
161
+
If the amount of data you are working with is very large, does not change frequently, and you need to run complex queries on it,
162
+
you could use a specialized analytical database such as [ClickHouse](https://clickhouse.com/) or [DuckDB](https://duckdb.org/).
163
+
Such databases can be used with SQLPage by using their [ODBC](https://en.wikipedia.org/wiki/Open_Database_Connectivity) drivers.
- [Microsoft SQL Server "Monitor and Tune for Performance"](https://learn.microsoft.com/en-us/sql/relational-databases/performance/monitor-and-tune-for-performance?view=sql-server-ver17)
0 commit comments