我試圖從數據庫中提取記錄。在樹結構中從數據庫提取記錄
我的數據庫表:
- 部門
- EmployeeDepartments (中間表的部門--->員工)
- 員工
- EmployeeLocations (中間表的員工 - - >位置)
- 地址
上表包含大量記錄。
我試了一下:
我的第一種方法:
using(SQLConnection con = new SQLConnection())
{
//Get all the departments
List<Department> departmentList = sp.GetAllDeparments();
foreach(Deparment deparment in departmentList){
//Get all employees
List<Employee> employeeList= sp.GetEmployeesByDepartment(departmentId);
foreach(Employee employee in employeeList){
//Get all locations
List<Location> locationList= sp.GetLocationsByEmployee(employeeId);
foreach(Location location in locationList){
//Code for location generation for PDF.
}
}
}
}
我的數據會根據以下的結構產生3210如果我在foreach循環中訪問數據庫,上面的代碼需要大約6分鐘的時間。
我的第二個辦法:
//Get all the departments
List<Department> departmentList = sp.GetAllDeparments();
//Get all employees
List<Employee> employeeList= sp.GetAllEmployees();
//Get all locations
List<Location> locationList= sp.GetAllLocations();
//Get EmployeeLocations List<EmployeeLocations> emplocationList=
sp.GetAllEmployeeLocations();
//Get EmployeeDepartments List<EmployeeDepartments> empDepList=
sp.GetAllEmployeeDepartments();
foreach(Deparment deparment in departmentList){
foreach(Employee employee in employeeList){
foreach(Location location in locationList){
}
}
}
我從上面提到的所有表獲取所有記錄,並在代碼的foreach使用LINQ操縱。因此,將時間從6分鐘縮短到4分鐘。
我的問題: 什麼是在儘可能最短的時間獲取數據的有效方法。在這種情況下?
我需要在上圖中操縱的數據。
你的問題不是關於iText或關於PDF。 –
我會說第二種方法,但結合正確的快速查找數據結構來查找部門員工和員工位置。數據獲取部分需要多長時間(在'foreach'之前)? –
即使我正在使用第二種方法,一次獲取數據並將其保存在內存中不是問題。但在做foreach時,我正在使用LambdaExpression進行提取。這需要時間。 – Vikash