-
In my screenshot is a table structure and relationships I've laid out within my database to make a virtual mailbox. This is necessary for my medical app so we can provide nearly the same functionality like GMail, but without data ever leaving the app to an external client. A brief scope of the tables and their purpose:
The relationship is as follows:mail_thread When a user opens their mailbox, I want the following to occur:The database is queried for any rows within mail_thread and only returns any results where mail_thread has a relational mail_thread_recipients record with their user ID. For each mail_thread record found, all related mail_message records will be gathered where mail_message has a relational mail_message_recipients record with their user ID. For each message found, all related mail_message_attachments records will be gathered. So with that scope out of the way, my questions are as follows:1.) Is this type of structure acceptable or is there a more efficient way of storing/fetching these records with the intended functionality provided? I've never stored data to this level of relational complexity with Phalcon, so I'm a bit lost on how to execute this properly. Any help would be greatly appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
My 2 cents The structure is very nicely done, normalized. More on improving this below. To get the data as efficiently as possible you will need to load the minimal amount of data that you want to display per action i.e. user clicks Inbox -> need to show all emails and who it was sent to. This way you can query only the mail thread and recipients and display that information. When a user clicks on a mail thread, you can further go to display each message and whether it has attachments or not. You get the point. You minimize queries based on the amount of information you need to display. Some minor reorganization might help you there also, for instance storing the number of attachments per message in the message table. This way you don't need to do a I would go with the query builder directly, to avoid n+1, unless this is the message display. Message display: You just query one message record. Using that, you can use Non message display (say inbox): Use the query builder to retrieve your data. If necessary, query first to get the IDs of the satellite tables that relate with your records, then use Finally, once this grows to the point that queries become slower, you might want to invest in a text search engine (Elasticsearch for instance) which will store all your data denormalized. The index of each row in Elasticsearch will be updated with events such as There was an app we had many moons ago, which displayed a list of records. We had 17 joins in that model. Type as you go search was 5 seconds per character. Using Elasticsearch it was reduced to 0.1 |
Beta Was this translation helpful? Give feedback.
My 2 cents
The structure is very nicely done, normalized. More on improving this below.
To get the data as efficiently as possible you will need to load the minimal amount of data that you want to display per action i.e. user clicks Inbox -> need to show all emails and who it was sent to.
This way you can query only the mail thread and recipients and display that information. When a user clicks on a mail thread, you can further go to display each message and whether it has attachments or not. You get the point. You minimize queries based on the amount of information you need to display. Some minor reorganization might help you there also, for instance storing the number of attachments per…