>Table Rows Number of Index >InvoiceEntete 500 336 2 >InvoiceDetail 14 496 307 3 >StatsFournProduit 6 226 957 2 >StatsFournProdDetail 7 010 503 3>We use a select cursor:
>DECLARE cur_id2 CURSOR READ_ONLY FOR > SELECT Convert(int,InvoiceEntete.compagnie_ln), > InvoiceEntete.code_client, > InvoiceDetail.produit_livre, > '00' as PackSeq, >codepromoTmp, >convert (int,DATEPART(year, InvoiceEntete.date_facture)), >convert (int,DATEPART(month, InvoiceEntete.date_facture)), > InvoiceEntete.date_facture, > InvoiceEntete.type_trans, > InvoiceEntete.code_promo, > InvoiceDetail.fournisseur_ln, > InvoiceDetail.esc_promo, > InvoiceEntete.statut, > InvoiceDetail.qte_livree, > InvoiceDetail.montant_net, > FROM InvoiceEntete > inner join InvoiceDetail > ON InvoiceEntete.compagnie_ln = >InvoiceDetail.compagnie_ln > and InvoiceEntete.order_id = >InvoiceDetail.order_id > and InvoiceEntete.order_suffixe = >InvoiceDetail.order_suffixe > where InvoiceEntete.compagnie_ln = >@CompanyId > and InvoiceDetail.order_id = @OrderNo > and InvoiceDetail.order_suffixe = >@OrderSuf > >Note: Looking at the execution plan of this cursor, it >use only Clustered Index Seek. (No table or index scan) > >Every time we loop in the cursor, we execute two SQL >queries to delete one row of the StatsFournProduit and >StatsFournProdDetail tables. > >SQL query we use > >For table StatsFournProduit > >SET @StrSql = N'DELETE FROM statsFournProduit > where companyid = @companyid > and code_client = @code_client > and produit_ln = @produit_livre > and PackSeq = @PackSeq > and annee = @annee > and fournisseur_ln = @fournisseur_ln ' > > >EXEC sp_executesql @StrSql, N'@code_client nvarchar(10), > > @produit_livre nvarchar(10), > @PackSeq >nvarchar(2), > @annee >int, > @CompanyId >Int, > > @fournisseur_ln nvarchar(10)', > > @code_client, > > @produit_livre, > @PackSeq, > @annee, > @CompanyId, > > @fournisseur_ln > >And for table StatsFournProdDetail > >SET @StrSqlDetail = N'DELETE FROM StatsFournProdDetail > where companyid = @companyid > and code_client = >@code_client > and produit_ln = >@produit_livre > and PackSeq = @PackSeq > and annee = @annee > and code_promo = >@codepromoTmp > and fournisseur_ln = >@fournisseur_ln ' > > EXEC sp_executesql @StrSqlDetail, >N'@code_client nvarchar(10), > > @produit_livre nvarchar(10), > @PackSeq >nvarchar(2), > @annee >int, > @CompanyId >Int, > > @codepromoTmp nvarchar(10), > > @fournisseur_ln nvarchar(10)', > > @code_client, > > @produit_livre, > @PackSeq, > @annee, > @CompanyId, > > @codepromoTmp, > > @fournisseur_ln>To delete 57 rows it takes 22 seconds. Our application