Click here to Skip to main content
15,905,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Updated but still slow.
C#
public static void Delete(int Id)
        {
            SQLiteConnection conn;
            using (conn = new SQLiteConnection(@"Data Source=C:\Users\job\Documents\Visual Studio 2013\Projects\BulkDeletSQLite\test.sqlite"))
            {
                conn.Open();               
                string delete = "DELETE FROM Person WHERE Id = @MyId";
                using (var command = new SQLiteCommand(delete,conn))
                {
                    command.Parameters.AddWithValue("@MyId", Id);
                    command.ExecuteScalar();           
                }
                conn.Close();
            }
        }

then:
C#
foreach (var item in db.Values.ToList())
{
    CreateTableForDB.Delete(item.Id);
}
Posted
Updated 3-Dec-14 6:24am
v5
Comments
PIEBALDconsult 3-Dec-14 12:34pm    
The updated code still has many problems. And ExecuteScalar is inappropriate in this context -- did IntelliSense trick you? I have updated my suggestion.
[no name] 3-Dec-14 12:44pm    
a.) Which part is slow(er),first or "then"?
b.) How many rows the table "Person" contains?
job mwa 3-Dec-14 13:51pm    
both and table person contains 200 rows.
[no name] 3-Dec-14 15:19pm    
If you make simply a select (instead of delete, but same "WHERE"), same slow?

Start by using the System.Diagnostics.StopWatch class[^] to look at that code in detail, and work out exactly where the slow part is: use several stopwatches, and check the connection construct and open, the transaction, and the actual delete itself all separately, then the Linq code. But I suspect it's the Linq: you are calling the Delete method repeatedly - which means you reapeated open a new connection, issue a transacted delete and close it again.
I would be tempted to open it once, issue a single delete using a "WHERE Id IN (list of Ids)" clause, and see how fast that worked. I suspect a lot, lot quicker...
 
Share this answer
 
Comments
job mwa 3-Dec-14 12:42pm    
what do you mean by "issue a single delete using a "WHERE Id IN (list of Ids)" clause"?? i think there maybe a solution here.
PIEBALDconsult 3-Dec-14 12:44pm    
http://www.codeproject.com/Tips/93248/SQL-Server-User-Defined-Table-Types-and-Table
(May not work with SQLlite.)
OriginalGriff 3-Dec-14 14:06pm    
WHERE IN works with SqLite:
http://www.tutorialspoint.com/sqlite/sqlite_where_clause.htm
PIEBALDconsult 3-Dec-14 14:10pm    
Yes, but a table-valued-parameter ?
OriginalGriff 3-Dec-14 14:26pm    
Haven't tried one in SqLite: but in this case string concatenation is safe anyway, since the IDs are all int.
0) Use a parameterized statement.
1) No reason to use a transaction for an individual operation.
2) Stop instantiating and disposing everything on each cycle; reuse what you have.
3) If you're passing in a connection, why throw it out?
4) WTF with that Linq? :omg:

5) Consider sending in multiple IDs rather than just one and then loop within the Delete method.

6) I also advise against deleting; generally, setting a status of some sort to indicate active/inactive is a better technique in the long run.

Rough and unready, but should give an idea:

C#
public static void Delete(IEnumerable<int> Idlist)
        {
            using (SQLiteConnection conn = new SQLiteConnection(blah blah) )
            {
                conn.Open();

                string delete = @"DELETE FROM Person WHERE Id =@id;";
                using (SQLiteCommand command = new SQLiteCommand(delete,conn))
                {
                    command.Parameters.Add ( new parameter ... ) ;
                    command.Transaction = conn.BeginTransaction() ;

                    foreach ( int id in Idlist )
                    {
                        command.Parameters [ 0 ].Value = id ;
                        command.ExecuteNonQuery(); // Consider saving the result
                    }
                    command.Transaction.Commit();
                }
                conn.Close();
            }
        }




See also:
Simplified Database Access via ADO.NET Interfaces[^]
 
Share this answer
 
v6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900