|
| 1 | + |
| 2 | +INSERT INTO blog_posts (title, description, icon, created_at, content) |
| 3 | +VALUES |
| 4 | + ( |
| 5 | + 'Performance Guide', |
| 6 | + 'Concrete advice on how to make your SQLPage webapp fast', |
| 7 | + 'bolt', |
| 8 | + '2025-10-31', |
| 9 | + ' |
| 10 | +# Performance Guide |
| 11 | +
|
| 12 | +SQLPage is [optimized](/performance) |
| 13 | +to allow you to create web pages that feel snappy. |
| 14 | +This guide contains advice on how to ensure your users never wait |
| 15 | +behind a blank screen waiting for your pages to load. |
| 16 | +
|
| 17 | +A lot of the advice here is not specific to SQLPage, but applies |
| 18 | +to making SQL queries fast in general. |
| 19 | +If you are already comfortable with SQL performance optimization, feel free to jump right to |
| 20 | +the second part of the quide: *SQLPage-specific advice*. |
| 21 | +
|
| 22 | +## Make your queries fast |
| 23 | +
|
| 24 | +The best way to ensure your SQLPage webapp is fast is to ensure your |
| 25 | +database is well managed and your SQL queries are well written. |
| 26 | +We''ll go over the most common database performance pitfalls so that you know how to avoid them. |
| 27 | +
|
| 28 | +### Choose the right database schema |
| 29 | +
|
| 30 | +#### Normalize (but not too much) |
| 31 | +
|
| 32 | +Your database schema should be made so that one piece of information |
| 33 | +is stored in only one place in the database. |
| 34 | +For instance, if you are modelling sales that happen in stores, the sales table should |
| 35 | +contain a foreign key to another table named stores. |
| 36 | +This way, when you need to display the list of stores in your application, you don''t have to |
| 37 | +run a slow `select distinct store from sales`, that would have to go through your millions of sales |
| 38 | +(*even if you have an index on the store column*), |
| 39 | +
|
| 40 | +### Use database indices |
| 41 | +
|
| 42 | +### Use (materialized) views |
| 43 | +
|
| 44 | +### Query performance debugging |
| 45 | +
|
| 46 | +## SQLPage-specific advice |
| 47 | +
|
| 48 | +The best way to make your SQLPage webapp fast is to make your queries fast. |
| 49 | +Sometimes, you just don''t have control over the database, and have to run slow queries. |
| 50 | +This section will help you minimize the impact to your users. |
| 51 | +
|
| 52 | +### Order matters |
| 53 | +
|
| 54 | +SQLPage executes the queries in your `.sql` files in order. |
| 55 | +It does not start executing a query before the previous one has returned all its results. |
| 56 | +So, if you have to execute a slow query, put it as far down in the page as possible. |
| 57 | +
|
| 58 | +#### No heavy computation before the shell |
| 59 | +
|
| 60 | +Every user-facing page in a SQLPage site has a [shell](/components?component=shell). |
| 61 | +
|
| 62 | +The first queries in any sql file (all the ones that come before the []) |
| 63 | +
|
| 64 | +#### Set variables just above their first usage |
| 65 | +
|
| 66 | +### Avoid recomputing the same data multiple times |
| 67 | +
|
| 68 | +### Reduce the number of queries |
| 69 | +
|
| 70 | +### Lazy loading |
| 71 | +
|
| 72 | +Use the card and modal components to load data lazily. |
| 73 | +
|
| 74 | +### Database connections |
| 75 | +
|
| 76 | +SQLPage uses connection pooling: it keeps multiple database connections opened, |
| 77 | +and reuses them for consecutive requests. When it does not receive requests for a long time, |
| 78 | +it closes idle connection. When it receives many requests, it opens new connection, |
| 79 | +but never more than the value specified by `max_database_pool_connections` in its |
| 80 | +[configuration](https://github.com/sqlpage/SQLPage/blob/main/configuration.md). |
| 81 | +You can increase the value of that parameter if your website has many concurrent users and your |
| 82 | +database is configured to allow opening many simultaneous connections. |
| 83 | +
|
| 84 | +### SQLPage performance debugging |
| 85 | +
|
| 86 | +When `environment` is set to `development` in its [configuration](https://github.com/sqlpage/SQLPage/blob/main/configuration.md), |
| 87 | +SQLPage will include precise measurement of the time it spends in each of the steps it has to go through before starting to send data |
| 88 | +back to the user''s browser. You can visualize that performance data in your browser''s network inspector. |
| 89 | +
|
| 90 | +You can set the `RUST_LOG` environment variable to `sqlpage=debug` to make SQLPage |
| 91 | +print detailed messages associated with precise timing for everything it does. |
| 92 | +'); |
0 commit comments