I am new to SQL and have trouble understanding why the Where statement is so inefficient.
A bit of background in the database. This is an SQL Compact Edition database that is used to store icons. A project can have multiple icons, each icon can have multiple paths, and each path is created from geometry, color, and opacity. There are approximately 2,000 icons, which leads to approximately 12,000 paths.

I am trying to create a query that returns only icons belonging to this project
SELECT Icons.Id, Icons.Name... etc FROM Icons
INNER JOIN Projects ON Icons.FK_ProjectId = Projects.Id
INNER JOIN IconDetails ON Icons.Id = IconDetails.FK_IconId
INNER JOIN Paths ON IconDetails.FK_PathId = Paths.Id
INNER JOIN Colours ON IconDetails.FK_ColourId = Colours.Id
WHERE (Icons.FK_ProjectId = 5)
It takes ~ 2.8 seconds. However, if I delete the bottom statement Where, it only takes ~ 0.3 seconds. Then I can use C # Linq to select all the icons where they belong to the project I want.
var iconTable = GetIconDataFromDatabase().Where(e => e.Project == projectName);
private List<IconData> GetIconDataFromDatabase()
{
var getAllIconsCommand = new SqlCeCommand(
return ReturnIconData(LOCAL_CONNECTION_STRING, getAllIconsCommand);
}
private List<IconData> ReturnIconData(string connectionString, SqlCeCommand command)
{
var IconDataToReturn = new List<IconData>();
using (var connection = new SqlCeConnection(connectionString))
{
command.Connection = connection;
using (command)
{
try
{
connection.Open();
using (SqlCeDataReader dataReader = command.ExecuteReader())
{
while (dataReader.Read())
{
IconDataToReturn.Add(new IconData
{
Id = int.Parse(dataReader["Id"].ToString().Trim()),
Project = dataReader["ProjectName"].ToString().Trim(),
Name = dataReader["Name"].ToString().Trim(),
Geometry = Geometry.Parse(dataReader["Geometry"].ToString().Trim()),
Colour = dataReader["Colour"].ToString().Trim(),
Opacity = double.Parse(dataReader["Opacity"].ToString().Trim()),
IsPathCompact = bool.Parse(dataReader["Compact"].ToString().Trim()),
ZOrder = int.Parse(dataReader["ZOrder"].ToString().Trim())
});
}
}
}
}
return IconDataToReturn;
}
, , .