unepwcmc/SAPI — pull request #165 by agnessa (2014-01-08 17:33:18+00:00)
Title: Trade reports optimisation

Pre-computed permits fields in shipments table
* instead of joining with permit tables when searching within the admin tool, precomputed permit number fields have been added to the shipments table:
-> import_permit_number, export_permit_number, origin_permit_number - these are concatenated, semicolon separated numbers of respective types for display in the "raw" report
-> permits_ids - these are all types of permits ids in one integer array for filtering
* the precomputed fields are updated when shipments are updated one by one ONLY WHEN USING THE CUSTOM SETTERS (i.e. assigning import_permits = array of permit objects will not recompute the permit fields)
* another way to recompute the permits is to use Sapi::StoreProcedures.rebuild_permit_numbers function, which will go through the entire shipments table (might come in handy for the import script)

Other speed improvements:
* indexes on the shipments table
* simplification of the shipments view (for raw reports)
* using the table rather than the view for other reports (this applies to the query constructed by Trade::Filter, which constitutes the inner subquery of any report - no need to use the view in the subquery)
* removed one apparently unnecessary join from comptab query