I have an MVC3 app which is using POCO classes separated into distinct layers. I have implemented a generic repo pattern as well.
I have a dashboard which uses Kendo UI charts for management to view real time stats. As the database is growing the dashboard is slower and slower. I found these reasons:
- Records grow daily
- Each chart calls to the repo to get all the rows to calculate stats with LINQ
- Automapper is creating my model to viewmodels
I would like to speed this up by using stored procedures in the database to get exactly the numbers I need for stats. I am not sure how I should implement this… even though it feels so wrong! Any tips?
Example Controller with Repo code
Public Class DashboardController
Inherits BaseController
Private ticketRepo As MaintenanceTicketsRepository
Public Sub New()
Me.ticketRepo = New MaintenanceTicketsRepository(New TicketContext)
End Sub
Function Chart_OpenItems() As ActionResult
Dim tickets As IList(Of MaintenanceTicket) = ticketRepo.GetAll().Include(Function(p) p.Priority).Include(Function(s) s.Status).OrderBy(Function(o) o.PriorityId).ToArray()
'Do some work with repo then dispose'
End Function
Function Chart_ClosedItems() As ActionResult
Dim tickets As IList(Of MaintenanceTicket) = ticketRepo.GetAll().Include(Function(p) p.Priority).Include(Function(s) s.Status).OrderBy(Function(o) o.PriorityId).ToArray()
'Do some work with repo then dispose'
End Function
End Class
Trying to answer your question. If you can reduce the amount of data going to your Model by using a stored procedure to aggregate the data before it leaves SQL Server then go for it. The actual implementation details will depend on what kind of ORM setup you have. If you can reduce the 1800 rows to >100 you will almost certainly see a speed increase.