优化查询mysql seach

时间:2016-04-20 21:00:10

标签: mysql sql

我有以下SQL但它的执行速度非常慢,大约需要45秒,表有1500万条记录,我该如何改进?

SELECT A.*, B.ESPECIE
FROM 
(
SELECT 
    A.CODIGO_DOCUMENTO,
    A.DOC_SERIE,A.DATA_EMISSAO,
    A.DOC_NUMERO,
    A.CF_NOME,
    A.CF_SRF,
    A.TOTAL_DOCUMENTO, 
    A.DOC_MODELO
FROM MOVIMENTO A  

WHERE
    A.CODIGO_EMPRESA = 1
        AND A.CODIGO_FILIAL = 5
        AND A.DOC_TIPO_MOVIMENTO = 1
        AND A.DOC_MODELO IN ('65','55')
        AND (A.CF_NOME LIKE '%TEXT_SEARCH%'
        OR A.CF_CODIGO LIKE 'TEXT_SEARCH%'
        OR A.CF_SRF LIKE 'TEXT_SEARCH%'
        OR A.DOC_SERIE LIKE 'TEXT_SEARCH%'
        OR A.DOC_NUMERO LIKE 'TEXT_SEARCH%')
ORDER BY A.DATA_EMISSAO DESC , A.CODIGO_DOCUMENTO DESC
LIMIT 0, 100
) A 
LEFT JOIN MODELODOCUMENTOFISCAL B ON A.DOC_MODELO = B.CODMODELO

1 个答案:

答案 0 :(得分:0)

对于此查询,我将从import javax.swing.*; import java.awt.*; import java.awt.event.*; public class EmployeesApplet extends JApplet implements ActionListener { public JButton sd = new JButton ("Salaried"); public JButton hr = new JButton ("Hourly"); public JButton cm = new JButton ("Commissioned"); public JButton cl = new JButton ("Clear"); private final int FIELDS = 8, FIELD_WIDTH = 20; private String[] strings = new String[FIELDS]; private TextFieldWithLabel[] tf = new TextFieldWithLabel[FIELDS]; private JTextArea ta = new JTextArea(5,25); String[] s = {"First Name", "Last Name", "Employee ID", "(a) Salaried: Weekly Salary", "(b1) Hourly 1: Rate Per Hour", "(b2) Hourly 2: Hours Worked" , "(c1) Commissioned: Rate", "(c2) Commissioned: Gross Sales" }; public void init() { this.setSize(420, 310); //---------------------- // Set up the Structure //---------------------- Container c = getContentPane(); JPanel f = new JPanel(new FlowLayout()); JPanel b = new JPanel(new BorderLayout(2,0)); JPanel glb = new JPanel(new GridLayout(8,1,0,2)); JPanel gtf = new JPanel(new GridLayout(8,1,0,2)); JPanel flb = new JPanel(new FlowLayout()); // Add FlowLayout to the container c.add(f); // Add BorderLayout to the FlowLayout f.add(b); //--------------------------------------- //Add JPanels to the BorderLayout regions //--------------------------------------- // Add JLables to GridLayout in West b.add(glb, BorderLayout.WEST); for (int i = 0; i < tf.length; i++) { tf[i] = new TextFieldWithLabel(s[i], FIELD_WIDTH); glb.add(tf[i].getLabel()); } // Add JTextFeilds to GridLayout in East b.add(gtf, BorderLayout.EAST); for (int i = 0; i < tf.length; i++) { tf[i] = new TextFieldWithLabel(s[i], FIELD_WIDTH); tf[i].getTextField(); gtf.add(tf[i].getTextField()); } // Add JButtons to FlowLayout in South b.add(flb, BorderLayout.SOUTH); flb.add(sd); flb.add(hr); flb.add(cm); flb.add(cl); sd.addActionListener(this); hr.addActionListener(this); cm.addActionListener(this); cl.addActionListener(this); // Add JTextArea and make it not editable f.add(ta); ta.setEditable(false); } //--------------------------------------- // Read all the JTextFields and // save the contents in a parallel array //--------------------------------------- private void readFields() { for (int i = 0; i < tf.length; i++) // or FIELDS strings[i] = tf[i].getText(); } private boolean fieldsExist(int i, int i2) { if(i == 0 && i2 == 3) // Checks Salaried worker { if(tf[0].getText() == null || tf[0].getText().length() == 0) { showStatus("First Name field is empty"); // Diplays error message in status area tf[0].getTextField().requestFocus(); // Places focus in JTextField return false; } else if(tf[1].getText() == null || tf[1].getText().length() == 0) { showStatus("Last Name field is empty"); tf[1].getTextField().requestFocus(); return false; } else if(tf[2].getText() == null || tf[2].getText().length() == 0) { showStatus("Employee ID field is empty"); tf[2].getTextField().requestFocus(); return false; } else if(tf[3].getText() == null || tf[3].getText().length() == 0) { showStatus("(a)Salried: Weekly Salary field is empty"); tf[3].getTextField().requestFocus(); return false; } else return true; } if(i == 0 && i2 == 2) // Checks Hourly worker { if(tf[0].getText() == null || tf[0].getText().length() == 0) { showStatus("First Name field is empty"); tf[0].getTextField().requestFocus(); return false; } else if(tf[1].getText() == null || tf[1].getText().length() == 0) { showStatus("Last Name field is empty"); tf[1].getTextField().requestFocus(); return false; } else if(tf[2].getText() == null || tf[2].getText().length() == 0) { showStatus("Employee ID field is empty"); tf[2].getTextField().requestFocus(); return false; } else return true; } if(i == 4 && i2 == 5) // Checks Hourly worker the second time { if(tf[4].getText() == null || tf[4].getText().length() == 0) { showStatus("(b1) Hourly 1: Rate Per Hour field is empty"); tf[5].getTextField().requestFocus(); return false; } else if(tf[5].getText() == null || tf[5].getText().length() == 0) { showStatus("(b2) Hourly 2: Hours Worked field is empty"); tf[5].getTextField().requestFocus(); return false; } else return true; } if(i == 0 && i2 == 2) // Checks Commissioned worker { if(tf[0].getText() == null || tf[0].getText().length() == 0) { showStatus("First Name field is empty"); tf[0].getTextField().requestFocus(); return false; } else if(tf[1].getText() == null || tf[1].getText().length() == 0) { showStatus("Last Name field is empty"); tf[1].getTextField().requestFocus(); return false; } else if(tf[2].getText() == null || tf[2].getText().length() == 0) { showStatus("Employee ID field is empty"); tf[2].getTextField().requestFocus(); return false; } else return true; } if(i == 6 && i2 == 7) // Checks Commissioned second time { if(tf[6].getText() == null || tf[6].getText().length() == 0) { showStatus("(c1)Commissioned: Rate field is empty"); tf[0].getTextField().requestFocus(); return false; } else if(tf[7].getText() == null || tf[7].getText().length() == 0) { showStatus("(c2)Commissioned: Ratefield is empty"); tf[1].getTextField().requestFocus(); return false; } else return true; } return false; } private boolean fieldsEmpty(int i, int i2, String[] a) { if(i == 4 && i2 == 7) // checks salaried { for (int index = 4; index <= 7; index++) { if(tf[index].getText().length() != 0) { showStatus( a[index] + " should be empty"); // Diplays error message in status area tf[index].getTextField().requestFocus(); // Places focus in JTextField return true; } else return false; } // end for } // end if if (i == 3 && i2 == 3) // checks hourly first time { if(tf[3].getText().length() != 0) { showStatus(a[3] + " field should be empty"); tf[3].getTextField().requestFocus(); return true; } } // end if if(i == 6 && i2 == 7) // checks hourly second time { for (int index = 6; index <= 7; index++) { if(tf[index].getText().length() != 0) { showStatus(a[index] + " field should be empty"); tf[index].getTextField().requestFocus(); return true; } } // end for } // end if if(i == 3 && i2 == 5) // checks commissioned { for (int index = 3; index <= 5; index++) { if(tf[index].getText().length() != 0) { showStatus(a[index] + " field should be empty"); tf[index].getTextField().requestFocus(); return true; } } // end for } // end if return false; } public void actionPerformed(ActionEvent e) { showStatus(""); if (e.getSource() == cl) // Executes clear button is clicked { for (int i = 0; i < FIELDS; i++) { tf[i].getTextField().setText(""); tf[0].getTextField().requestFocus(); } } // End clear if if (e.getSource() != cl) { if(e.getSource() == sd) // checks for salaried employee { showStatus("Salaried"); fieldsExist(0,3); fieldsEmpty(4,7, s); ta.append(Salaried.toString()); } // end salaried if(e.getSource() == hr) // checks for hourly employee { showStatus("Hourly"); fieldsExist(0,2); fieldsExist(4,5); fieldsEmpty(3,3, s); fieldsEmpty(6,7, s); } // end hourly if(e.getSource() == cm) // checks for commissioned employee { showStatus("Commissioned"); fieldsExist(0,2); fieldsExist(6,7); fieldsEmpty(3,5, s); } // end commisssioned } // end if } // End of actionPerformed } MOVIMENTO(CODIGO_EMPRESA, CODIGO_FILIAL, DOC_MODELO)上的索引开始。

这应该加快查询速度。

如果不是,您可能需要考虑全文搜索来处理MODELODOCUMENTOFISCAL(CODMODELO)条款。我注意到你只在其中一个模式的开头有一个通配符。这是故意的吗?