峰峦@叠嶂 2024-09-09 09:36 采纳率: 99.5%
浏览 15
已结题

需要优化SQL查询语句,请看一下

请教一下,我在文本框的回车事件中,针对产品码,对产品的相关信息进行查询,但有时运行会有查询卡顿的情况发生,若直接将下述查询语句在SQLserver中查询,就能迅速查询出结果,请问怎样优化,能提升性能呢,有建议用存储过程或视图,也有建议用索引或缓冲,请大家给一下建议,谢谢;


 private void txtCode20_KeyUp(object sender, KeyEventArgs e)
 {
     
     if (e.KeyCode == Keys.Enter)
     {
         string connectionString = "server=192.168.4.200;database=BCM30_DrugAdmin;user=sa;password=sskyadmin1!";

         string query = @"SELECT productName,spec,packageSpec, code20, batchNo, parentCode, orderCode, customerName, orderType, createTime, updateTime 
                     FROM (

                     SELECT pro.productName as productName,pro.spec as spec,pro.packageSpec as packageSpec,
                            dr.code20 as code20 ,dr.batchNo as batchNo, dr.parentCode2 as parentCode, wo.orderCode as orderCode ,wd.customerName as customerName,wd.orderType as orderType,wo.createTime as createTime,wo.updateTime as updateTime FROM  dbo.da_codeRelation_193815500501 dr
                     JOIN dbo.da_warehouseOutCode wo ON wo.code20 IN (dr.code20, dr.parentCode2 )
                     JOIN dbo.bcm_warehouseOrder wd on  wo.orderCode = wd.orderCode
                     JOIN dbo.da_product pro  on pro.subTypeNo = dr.subTypeNo

                     WHERE dr.code20 = @Code20 --心可舒96片(注意,这个的大箱码是parentCode2)

                     union

                     SELECT pro.productName as productName,pro.spec as spec,pro.packageSpec as packageSpec,
                            dr.code20 as code20 ,dr.batchNo as batchNo, dr.parentCode3 as parentCode, wo.orderCode as orderCode,wd.customerName as customerName,wd.orderType as orderType ,wo.createTime as createTime ,wo.updateTime as updateTime FROM  dbo.da_codeRelation_193815500202 dr
                     JOIN dbo.da_warehouseOutCode wo ON wo.code20 IN (dr.code20, dr.parentCode2, dr.parentCode3)
                     JOIN dbo.bcm_warehouseOrder wd on  wo.orderCode = wd.orderCode 
                     JOIN dbo.da_product pro  on pro.subTypeNo = dr.subTypeNo

                     WHERE dr.code20 = @Code20 --心可舒48union

                     SELECT pro.productName as productName,pro.spec as spec,pro.packageSpec as packageSpec,
                            dr.code20 as code20 ,dr.batchNo as batchNo, dr.parentCode3 as parentCode, wo.orderCode as orderCode,wd.customerName as customerName,wd.orderType as orderType ,wo.createTime as createTime ,wo.updateTime as updateTime FROM  dbo.da_codeRelation_193815500402 dr
                     JOIN dbo.da_warehouseOutCode wo ON wo.code20 IN (dr.code20, dr.parentCode2, dr.parentCode3)
                     JOIN dbo.bcm_warehouseOrder wd on  wo.orderCode = wd.orderCode 
                     JOIN dbo.da_product pro  on pro.subTypeNo = dr.subTypeNo

                     WHERE dr.code20 = @Code20 --心可舒72union

                     SELECT pro.productName as productName,pro.spec as spec,pro.packageSpec as packageSpec,
                            dr.code20 as code20 ,dr.batchNo as batchNo , dr.parentCode2 as parentCode, wo.orderCode as orderCode,wd.customerName as customerName,wd.orderType as orderType , wo.createTime as createTime,wo.updateTime as updateTime FROM  dbo.da_codeRelation_191925600201 dr
                     JOIN dbo.da_warehouseOutCode wo ON wo.code20 IN (dr.code20, dr.parentCode2) 
                     JOIN dbo.bcm_warehouseOrder wd on  wo.orderCode = wd.orderCode 
                     JOIN dbo.da_product pro  on pro.subTypeNo = dr.subTypeNo

                     WHERE dr.code20 = @Code20 --心可舒胶囊24union 
  
                     SELECT pro.productName as productName,pro.spec as spec,pro.packageSpec as packageSpec,
                            dr.code20 as code20 ,dr.batchNo as batchNo , dr.parentCode3 as parentCode , wo.orderCode as orderCode,wd.customerName as customerName,wd.orderType as orderType ,wo.createTime as createTime,wo.updateTime as updateTime FROM  dbo.da_codeRelation_191925600102  dr
                     JOIN dbo.da_warehouseOutCode wo ON wo.code20 IN (dr.code20, dr.parentCode2,dr.parentCode3)
                     JOIN dbo.bcm_warehouseOrder wd on  wo.orderCode = wd.orderCode 
                     JOIN dbo.da_product pro  on pro.subTypeNo = dr.subTypeNo

                     WHERE dr.code20 = @Code20 ---心可舒胶囊72union

                     SELECT pro.productName as productName,pro.spec as spec,pro.packageSpec as packageSpec,
                            dr.code20 as code20 ,dr.batchNo as batchNo , dr.parentCode3 as parentCode , wo.orderCode as orderCode,wd.customerName as customerName,wd.orderType as orderType ,wo.createTime as createTime,wo.updateTime as updateTime FROM  dbo.da_codeRelation_193815500302 dr
                     JOIN dbo.da_warehouseOutCode wo ON wo.code20 IN (dr.code20, dr.parentCode2,dr.parentCode3)
                     JOIN dbo.bcm_warehouseOrder wd on  wo.orderCode = wd.orderCode
                     JOIN dbo.da_product pro  on pro.subTypeNo = dr.subTypeNo

                     WHERE dr.code20 = @Code20-----心可舒异形片0.62                         

                     union 
 
                     SELECT pro.productName as productName,pro.spec as spec,pro.packageSpec as packageSpec,
                            dr.code20 as code20 ,dr.batchNo as batchNo , dr.parentCode2 as parentCode , wo.orderCode as orderCode,wd.customerName as customerName,wd.orderType as orderType ,wo.createTime as createTime,wo.updateTime as updateTime FROM  dbo.da_codeRelation_922026600101  dr
                     JOIN dbo.da_warehouseOutCode wo ON wo.code20 IN (dr.code20, dr.parentCode2)
                     JOIN dbo.bcm_warehouseOrder wd on  wo.orderCode = wd.orderCode 
                     JOIN dbo.da_product pro  on pro.subTypeNo = dr.subTypeNo

                     WHERE dr.code20 = @Code20-----脑血疏10*10ML

                     union 

                     SELECT pro.productName as productName,pro.spec as spec,pro.packageSpec as packageSpec,
                            dr.code20 as code20 ,dr.batchNo as batchNo , dr.parentCode2 as parentCode , wo.orderCode as orderCode,wd.customerName as customerName,wd.orderType as orderType ,wo.createTime as createTime,wo.updateTime as updateTime FROM  dbo.da_codeRelation_921996000101  dr
                     JOIN dbo.da_warehouseOutCode wo ON wo.code20 IN (dr.code20, dr.parentCode2)
                     JOIN dbo.bcm_warehouseOrder wd on  wo.orderCode = wd.orderCode
                     JOIN dbo.da_product pro  on pro.subTypeNo = dr.subTypeNo

                     WHERE dr.code20 = @Code20---牛黄益金片1:150

                     union  
  
                     SELECT pro.productName as productName,pro.spec as spec,pro.packageSpec as packageSpec,
                            dr.code20 as code20 ,dr.batchNo as batchNo , dr.parentCode2 as parentCode , wo.orderCode as orderCode,wd.customerName as customerName,wd.orderType as orderType ,wo.createTime as createTime,wo.updateTime as updateTime FROM  dbo.da_codeRelation_921996000102  dr
                     JOIN dbo.da_warehouseOutCode wo ON wo.code20 IN (dr.code20, dr.parentCode2)
                     JOIN dbo.bcm_warehouseOrder wd on  wo.orderCode = wd.orderCode 
                     JOIN dbo.da_product pro  on pro.subTypeNo = dr.subTypeNo

                     WHERE dr.code20 = @Code20---牛黄益金片1:200
                     
                      union 

                      SELECT pro.productName as productName,pro.spec as spec,pro.packageSpec as packageSpec,
                             dr.code20 as code20 ,dr.batchNo as batchNo, dr.parentCode2 as parentCode2, wo.orderCode as orderCode ,wd.customerName as customerName,wd.orderType as orderType,wo.createTime as createTime,wo.updateTime as updateTime FROM  dbo.da_codeRelation_922026300101 dr
                     JOIN dbo.da_warehouseOutCode wo ON  wo.code20 IN (dr.code20, dr.parentCode2)
                     JOIN dbo.bcm_warehouseOrder wd on  wo.orderCode = wd.orderCode 
                     JOIN dbo.da_product pro  on pro.subTypeNo = dr.subTypeNo
                     
                     WHERE dr.code20 = @Code20 ) as subquery ---抗病毒口服液"; 

         try
         {

             using (SqlConnection Conn = new SqlConnection(connectionString))
             {
                 DataTable sumdt = new DataTable();

                 string Code20 = txtCode20.Text;

                 using (SqlCommand Cmd = new SqlCommand(query, Conn))
                 {
                     Cmd.Parameters.AddWithValue("@Code20", Code20);

                     SqlDataAdapter sda = new SqlDataAdapter(Cmd);

                 
                     dataGridView1.DataSource = null;

                     sda.Fill(sumdt);

                 }

                 dataGridView1.DataSource = sumdt;

                 dataGridView1.Columns[0].HeaderText = "产品名称";
                 dataGridView1.Columns[1].HeaderText = "规格";
                 dataGridView1.Columns[2].HeaderText = "包装规格";
                 dataGridView1.Columns[3].HeaderText = "小盒码";
                 dataGridView1.Columns[4].HeaderText = "批号";
                 dataGridView1.Columns[5].HeaderText = "大箱码";
                 dataGridView1.Columns[6].HeaderText = "销售单号";
                 dataGridView1.Columns[7].HeaderText = "客户名称";
                 dataGridView1.Columns[8].HeaderText = "类型";
                 dataGridView1.Columns[9].HeaderText = "出库日期";
                 dataGridView1.Columns[10].HeaderText = "出库时间";

                 foreach (DataRow row in sumdt.Rows) //加上出库单的类型
                 {
                     string type = Convert.ToString(row["orderType"]);

                     if (type == "OA") { row["orderType"] = "销售出库"; }
                     if (type == "IB") { row["orderType"] = "退货入库"; }
                 }

             }
         }

         catch (Exception ex)
         {
             MessageBox.Show(ex.Message);
         }
     }

 }

  • 写回答

2条回答 默认 最新

  • hzlcsv 2024-09-09 16:52
    关注

    看上去可以先union药品表,再join da_warehouseOutCode、bcm_warehouseOrder、da_product

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月20日
  • 已采纳回答 9月12日
  • 创建了问题 9月9日

悬赏问题

  • ¥20 三极管1000倍放大电路
  • ¥15 vscode报错如何解决
  • ¥15 前端vue CryptoJS Aes CBC加密后端java解密
  • ¥15 python随机森林对两个excel表格读取,shap报错
  • ¥15 基于STM32心率血氧监测(OLED显示)相关代码运行成功后烧录成功OLED显示屏不显示的原因是什么
  • ¥100 X轴为分离变量(因子变量),如何控制X轴每个分类变量的长度。
  • ¥30 求给定范围的全体素数p的(p-2)/p的连乘积值
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术