View Optimization

Hi,

I have requirement to optimize a view. The structure of view is available as below with multiple select commands based on code. 

SELECT
`S`.`ID` AS `ID`,
(select `TEST_MASTER_DATA`.`LABEL` from `TEST_MASTER_DATA` where ((`TEST_MASTER_DATA`.`CODE` = `S`.`CODE`) and (`TEST_MASTER_DATA`.`FIELD` = 5)))
AS `STATUS_CODE`,

`S`.`INFO` AS `INFO`

FROM `Test_Sales` `S`
LEFT JOIN `Test_FollowUp` `F` ON `S`.`ID`= `F`.`ID` ;

I have multiple select commands with in the view to fetch the label from master data based on the code since we are saving code in the sales table.

Since we are triggering multiple calls with reference data the view is taking too much time in the user interface level.

Is there any way to optimize the view to improve the performance.

Thanks in Advance

  Discussion posts and replies are publicly visible

Parents
  • You will need to add indexes as others have suggested on the Join Columns. In general, if queries or views are being performed on a table where filtering or joining needs to occur you should take the time to research the proper syntax and which indexes are appropriate to use (ie fulltext vs index)

    If you are using MySQL, queries don't always automatically use the best indexes available, but I have had success in utilizing the 'USE INDEX' syntax

    If you are going to be filtering on the view (ie. select * from view where condition=true) I would suggest instead to use a stored procedure to construct a sql query for this data. When querying against a view the indexes on the underlying tables aren't used and you will run into performance issues.

    You should also utilize the EXPLAIN SELECT on your query to find out if any full table scans are being performed and try to limit those
Reply
  • You will need to add indexes as others have suggested on the Join Columns. In general, if queries or views are being performed on a table where filtering or joining needs to occur you should take the time to research the proper syntax and which indexes are appropriate to use (ie fulltext vs index)

    If you are using MySQL, queries don't always automatically use the best indexes available, but I have had success in utilizing the 'USE INDEX' syntax

    If you are going to be filtering on the view (ie. select * from view where condition=true) I would suggest instead to use a stored procedure to construct a sql query for this data. When querying against a view the indexes on the underlying tables aren't used and you will run into performance issues.

    You should also utilize the EXPLAIN SELECT on your query to find out if any full table scans are being performed and try to limit those
Children
No Data