Trigger to insert unique data into other table (having more than 40 millions of records) - MYSQL

Hi All,
i am facing impact of trigger in MYSQL, scenario is this:
I am Having one table with duplicate records that is consist of (eid,tin,status and some other columns are also there but i need only these three).  there is another table which is having same these three columns (eid, tin, status).
eid and tin will be same for given combination only status will be different i.e.
1245 23 0
1245 23 1
1245 23 5
1233 33 3
1211 24 2
1211 24 5
so as per above example i have to feed data into other table as
1245 23 0
1233 33 3
1211 24 5
priority of status is like 0 will be inserted if that is present in record otherwise it will be decrease from 5 to 1.
so i have designed trigger for this which will insert data after reading each row, but it is taking around 6.5 minutes for inserting 300000 records. so is there any other way to improve performance  for this mysql program.
DELIMITER $$
CREATE
    /*[DEFINER = { user | CURRENT_USER }]*/
    TRIGGER `kyr_log`.`upd_status` AFTER INSERT
    ON `kyr_log`.`kyrlog_bup`
    FOR EACH ROW
    BEGIN
DECLARE v_eid VARCHAR(28);
DECLARE v_status INT(11);
SELECT kyrl_eid,kyrl_status INTO v_eid,v_status FROM kyrlog_bup ORDER BY kyrl_id DESC LIMIT 1;
   IF v_eid NOT IN (SELECT kyrl_eid FROM update_status.new_status) THEN
INSERT INTO update_status.new_status(kyrl_eid,kyrl_tin,kyrl_status)
SELECT kyrl_eid,kyrl_tin,kyrl_status FROM kyrlog_bup ORDER BY kyrl_id DESC LIMIT 1;
   ELSE IF v_status=2 THEN
IF v_status > ANY (SELECT kyrl_status FROM kyrlog_bup WHERE kyrl_eid=v_eid AND kyrl_status<>0) THEN
          UPDATE update_status.new_status SET kyrl_status=v_status WHERE kyrl_eid=v_eid;
END IF;
   ELSE IF v_status=3 THEN
IF v_status > ANY (SELECT kyrl_status FROM kyrlog_bup WHERE kyrl_eid=v_eid AND kyrl_status<>0) THEN
          UPDATE update_status.new_status SET kyrl_status=v_status WHERE kyrl_eid=v_eid;  
END IF;
   ELSE IF v_status=4 THEN
IF v_status > ANY (SELECT kyrl_status FROM kyrlog_bup WHERE kyrl_eid=v_eid AND kyrl_status<>0) THEN
           UPDATE update_status.new_status SET kyrl_status=v_status WHERE kyrl_eid=v_eid; 
END IF;
   ELSE IF v_status=5 THEN
IF v_status > ANY (SELECT kyrl_status FROM kyrlog_bup WHERE kyrl_eid=v_eid AND kyrl_status<>0) THEN
           UPDATE update_status.new_status SET kyrl_status=v_status WHERE kyrl_eid=v_eid;
END IF;
           ELSE IF v_status=0 THEN
UPDATE update_status.new_status SET kyrl_status=v_status WHERE kyrl_eid=v_eid;            
          END IF;
       END IF;
       END IF;
       END IF;
       END IF;
       END IF;
    END;
$$
DELIMITER ;
please suggest me if there is  possibility of any other solution.
thanks

actually you didn't have seen discussion on this link , there are many discussion related to MYSQL . and mysql is owned by oracle. so i post it here.
thanks for suggestion

Similar Messages

  • Can i  create IN/UP/DE Trigger on a existing table having more than 2L rows

    Hi All,
    Can i create a INSERT/UPDATE/DELETE Trigger on a existing table having more than 2 lacs records ? if yes (is that works for only some kind of triggers? or for all ?). if not, tell me the reasons and limitations with guidelines.
    Thanks in Advance.

    Hi,
    Sure; you can create new triggers for tables that already have rows. The type of trigger and the number of rows already in the table don't matter.
    Try it. If the trigger works with a small table in your Development database, then there's no reason to expect it won't work on a larger table in your Production datbase.

  • Insert the data into two tables at a time.

    Hi ,
    i have these two tables
    create table [dbo].[test1](
    [test1_id] [int] identity(1,1) primary key,
    [test2_id] [int] not null
    create table [dbo].[test2](
    [test2_id] [int] identity(1,1) primary key,
    [test1_id] [int] not null
    alter table [dbo].[test1]
    add constraint [fk_test1_test2_id] foreign key([test2_id])
    references [dbo].[test2] ([test2_id])
    alter table [dbo].[test2] add constraint [fk_test2_test2_id] foreign key([test1_id])
    references [dbo].[test1] ([test1_id])
    I want to insert the data into two tables in one insert statement. How can i do this using T-SQL ?
    Thanks in advance.

    You can INSERT into both tables within one Transaction but not in one statement. By the way, you would need to alter your dbo.Test1 table to allow null for first INSERT test2_id column
    See sample code below:
    CREATE TABLE #test1(test1_ID INT IDENTITY(1,1),test2_id INT NULL)
    CREATE TABLE #test2(test2_ID INT IDENTITY(1,1),test1_ID INT)
    DECLARE @Test1dentity INT
    DECLARE @Test2dentity INT
    BEGIN TRAN
    -- Insert NULL as test2_ID value is unknown
    INSERT INTO #test1(test2_ID)
    SELECT NULL;
    -- get inserted identity value
    SET @Test1dentity = SCOPE_IDENTITY();
    INSERT INTO #test2(test1_ID)
    SELECT @Test1dentity;
    -- get inserted identity value
    SET @Test2dentity = SCOPE_IDENTITY();
    -- Update test1 table
    UPDATE #test1
    SET test2_ID = @Test2dentity
    WHERE test1_ID = @Test1dentity;
    COMMIT
    SELECT * FROM #test1;
    SELECT * FROM #test2;
    -- Drop temp tables
    IF OBJECT_ID('tempdb..#test1') IS NOT NULL
    BEGIN
    DROP TABLE #test1
    END
    IF OBJECT_ID('tempdb..#test2') IS NOT NULL
    BEGIN
    DROP TABLE #test2
    END
    web: www.ronnierahman.com

  • How to insert/Update date into Dictionary Table?

    Dear Experts,
    I have created a Dictionary Project and Deployed.I think the table had created in portal database.
    now i want to insert some data into that table?
    Can you plz tell me how to access the table using Abstract Portal Component using DynPage.
    Waiting for ur replies...
    Thanks and Regards,
    Visweswar.

    Hi
    Can u share the Logic???

  • How to insert  Legacy data into QP_RLTD_MODIFIERS table?

    How to insert  Legacy data into QP_RLTD_MODIFIERS table in R12 instance.

    I would use the API QP_Modifiers_PUB.Process_Modifiers for pushing legacy pricing data into R12.  QP_RLTD_MODIFIERS is only used for certain types of discounts (in my prod environnment, only promos have data in this table).

  • How to insert text data into temp tables....

    Dear All,
    I have one notepad with three columns, first column is segment1, second column is segment2 & third column is price list....
    and there is no delimiter and exact spaces..(i.e, zizak fomat)
    Ex:-
    xx yy 00009999
    kk mmmm 00009333
    Data is available like above example...So, I need to insert this data into one table.(2LAKSHS OF RECORDS AVAILABLE IN THAT NOTEPAD)
    So, Any can one help me, how to insert this kind of text data into temparory table...
    Regards
    Krishna
    Edited by: user12070109 on May 29, 2010 9:48 PM
    Edited by: user12070109 on May 29, 2010 9:49 PM

    Hello,
    What manu suggested this can be done through oracle forms.
    If as i read your last post you are using that in database it will not work in db. Try to use the same process in oracle forms will work by making some changes.
    And if you don't want to use forms then there is one way using SQL LOADER. It required control file to execute for uploading data.
    See the below link.
    http://www.orafaq.com/wiki/SQL*Loader_FAQ
    In this example its showing filename.csv you can use your file name like yourfilename.txt.
    So your control file will look like this...
    load data
    infile 'file_path\file_name.txt'
    into table table_name  -- use actual table name where you want to upload data
    fields terminated by " "  -- Here using spaces as you mentioned           
    (column1, column2, column3)  -- Here use the three column names of tableAnd after creating control file with the above code. You can call it in command prompt like this
    sqlldr username/password control=control_file_path\control_file_name.ctl log=log_file_path\log_file_name.log
    or
    sqlldr username/password@dbconnection control=control_file_path\control_file_name.ctl log=log_file_path\log_file_name.log
    Before doing this practice make sure SQLLDR.exe availabe in the machine where you have to execute. Otherwise you will have to install db client for using sqlldr.exe
    -Ammad

  • Inserting the data into other schema..

    Hi,
    Can any body help me to write the pl/sql code to insert the data into other schema
    eg: A and B two schemas are there
    In B there is a table with the name EM_Rep. When i was writting the pl sql code to insert the data into B.EM_Rep from A Schema it is throughing an error like
    PL/SQL: ORA-00942: table or view does not exist
    Note: I have given the grant like
    connected to user b
    sqL> grant insert on EM_Rep to A;
    PROCEDURE INS_EM()
    AS
    BEGIN
    insert into B.EM_Rep values('999999','test');
    END;
    its throughing an error like table doesnot exist....
    can any body tell ..how to write ...
    Thanks,

    It worked for me
    SQL> grant connect, resource to a identified by a;
    Grant succeeded.
    SQL> grant connect, resource to b identified by b;
    Grant succeeded.
    SQL> connect b/b
    Connected.
    SQL> create table em_rep (col1 varchar2(10), col2 varchar2(10));
    Table created.
    SQL> grant insert on em_rep to a;
    Grant succeeded.
    SQL> connect a/a
    Connected.
    SQL> create or replace
      2  PROCEDURE INS_EM
      3  AS
      4  BEGIN
      5  insert into B.EM_Rep values('999999','test');
      6  END;
      7  /
    Procedure created.
    SQL> exec ins_em;
    PL/SQL procedure successfully completed.
    SQL> commit;
    Commit complete.
    SQL> connect b/b
    Connected.
    SQL> select * from em_rep;
    COL1       COL2
    999999     test
    SQL>

  • Trouble with the SQL smt to :list tables having more than 1000 rows

    Please I trying to list only tables having more than 1000 rows, but the sql stmt below doesn't work, can someone gives me a tips
    select table_name from user_tables where table_name in ( select table_name from user_tables where rownum > 1000 ) : The result is no rows!
    But I know that I have at lest 50 tables having more than 1000 rows
    Thanks a lot for the help

    If your tables are reasonably analyzed, then you can simply query:
    SELECT table_name,
           num_rows
      FROM user_tables
    WHERE num_rows >= 1000This will give you quite a reasonable estimate.
    Otherwise you have to go for dynamic sql or use the data dictionary to help you generate suitable scripts ....

  • What is the best practice for inserting (unique) rows into a table containing key columns constraint where source may contain duplicate (already existing) rows?

    My final data table contains a two key columns unique key constraint.  I insert data into this table from a daily capture table (which also contains the two columns that make up the key in the final data table but are not constrained
    (not unique) in the daily capture table).  I don't want to insert rows from daily capture which already exists in final data table (based on the two key columns).  Currently, what I do is to select * into a #temp table from the join
    of daily capture and final data tables on these two key columns.  Then I delete the rows in the daily capture table which match the #temp table.  Then I insert the remaining rows from daily capture into the final data table. 
    Would it be possible to simplify this process by using an Instead Of trigger in the final table and just insert directly from the daily capture table?  How would this look?
    What is the best practice for inserting unique (new) rows and ignoring duplicate rows (rows that already exist in both the daily capture and final data tables) in my particular operation?
    Rich P

    Please follow basic Netiquette and post the DDL we need to answer this. Follow industry and ANSI/ISO standards in your data. You should follow ISO-11179 rules for naming data elements. You should follow ISO-8601 rules for displaying temporal data. We need
    to know the data types, keys and constraints on the table. Avoid dialect in favor of ANSI/ISO Standard SQL. And you need to read and download the PDF for: 
    https://www.simple-talk.com/books/sql-books/119-sql-code-smells/
    >> My final data table contains a two key columns unique key constraint. [unh? one two-column key or two one column keys? Sure wish you posted DDL] I insert data into this table from a daily capture table (which also contains the two columns that make
    up the key in the final data table but are not constrained (not unique) in the daily capture table). <<
    Then the "capture table" is not a table at all! Remember the fist day of your RDBMS class? A table has to have a key.  You need to fix this error. What ETL tool do you use? 
    >> I don't want to insert rows from daily capture which already exists in final data table (based on the two key columns). <<
    MERGE statement; Google it. And do not use temp tables. 
    --CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
    in Sets / Trees and Hierarchies in SQL

  • Trying to insert CLOB data into Remote Table..

    Hi everyone,
    I think this question had already posted.But i am not able to figure out this problem..
    what i am trying to do is
    I have a table in the remote database with a CLOB column like this
    REMOTE_TABLE
    ============
    REMOTE_TABLE_ID (Populated with sequence)
    REMOTE_CLOB CLOB
    In my Local database i have to write a Procedure to gather some information on a particular record (My Requirement) and save that CLOB in the REMOTE_TABLE.
    I built that procedure like this
    Declare
    var_clob CLOB; /* I need to processs several records and keep all data in a clob
    begin
    /***** Processed several records in a local database and stored in the variable var_clob which i need to insert into remote database ****/
    Insert into remote_table@remote values (remote_table_seq.nextval,var_clob);
    /*** when i try to execute the above command i am getting the following error
    ORA-06550: line 6, column 105:
    PL/SQL: ORA-22992: cannot use LOB locators selected from remote tables
    ORA-06550: line 6, column 1:
    PL/SQL: SQL Statement ignored *****/
    /***For a test i created the same table in local db****/
    Insert into local_table values (local_table_seq.nextval,var_clob);
    It is working fine and i am able to see the entire CLOB what i want.
    surprisingly if i pass some value instead of a varibale to the remote table like the following..
    Insert into remote_table@remote values (remote_table_seq.nextval,'Hiiiiiiiii');
    It is working fine...
    I tried the following too..
    decalre
    var_clob clob;
    begin
    var_clob := 'Hiiiiiiiiiiiiiii';
    Insert into remote_table@remote (remote_table_id) values (1);
    commit;
    update remote_table@remote set remote_clob = var_clob where remote_table_id = 1;
    commit;
    end
    I am getting the following error..
    ORA-22922: nonexistent LOB value
    ORA-02063: preceding line from CARDIO
    ORA-06512: at line 6
    Could someone please help me in fixing this issue..I need to process all the data to a variable like var_clob and insert that clob into remote table..
    Thanks in advance..
    phani

    Go to http://asktom.oracle.com and search for clob remote table
    also docs contain quite lot of info:
    http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14249/adlob_working.htm#sthref97
    Gints Plivna
    http://www.gplivna.eu

  • General Scenario- Adding columns into a table with more than 100 million rows

    I was asked/given a scenario, what issues do you encounter when you try to add new columns to a table with more than 200 million rows? How do you overcome those?
    Thanks in advance.
    svk

    For such a large table, it is better to add the new column to the end of the table to avoid any performance impact, as RSingh suggested.
    Also avoid to use any default on the newly created statement, or SQL Server will have to fill up 200 million fields with this default value. If you need one, add an empty column and update the column by using small batches (otherwise you lock up the whole
    table). Add the default after all the rows have a value for the new column.

  • Logical table having more than one Logical table sources

    Hi ,
    Is it possible a logical table has 2 logical table sources and there is no join between the underlying table of logical table sources?
    What will happen if we create the request which contains one attribute from each source?
    Will BI server return records with Cartesian join?
    Is it mandatory if logical table is having more than one logical table sources(exp. Logical table sources are X,Y and underlying table is A,B respectively),
    The underlying tables (A&B) must be joined ?
    Can someone explain the logic behind the logical table sources? How does it work?
    Any reference of document will be appreciable.
    Thanks,Ashish

    Hi Stijn,
    Thanks for response.
    You are saying that Oracle BI server will run a query against both tables and combine the results. But if there is no possibilty of any type of join, how BI server will combine the results from both tables.
    I have a scenario like below -
    Suppose i have 2 tables A and B at physical layer. Both table contains the information of any common subject area.
    But there is no attribute is common between A and B so not any kind of join possible between two.
    Now if i create a logical table with 2 logical table source A and B and create any request having one column from A and one from B ,
    What will happen? How BI server will combine the result? Will BI server not result records after Cartesian join.
    How can i model this scenario?

  • I can't insert my data into the table - help please

    Hello everyone,
    I have a trouble about JSP. I'm using Java2 SDK,JSP 1.2,Mysql and TOMCAT.
    I want to do this. A user has to enter the nicno,into the html form(EnterNicInter.jsp),to verify if it is already exist or not. If it is exist in the db, an error msg(Record already exist) must be displayed. If not, then the page "InterviewForm.jsp" displayed. This part is working.
    But, I can't save these details into the db by clicking submit button int the InterviewForm.jsp.
    I wrote a javaBean for this task. Here is it.
    CandidateMgr.java
    package hrm;
    import java.io.*;
    import java.sql.*;
    public class CandidateMgr
         private String nicno;
            private String title;
            private String firstName;
         private String civilStatus;
            private String tele;
            private String eduQua;
         // Constructor
         public CandidateMgr()
              this.nicno = nicno;
              this.title = title;
              this.firstName = firstName;
              this.civilStatus= civilStatus;
              this.tele = tele;
              this.eduQua = eduQua;
              //getters & setters.
               // Initializes the connection and statements
            public void initConnection() {
                    if (connection == null) {
                 try {
                          String sql;
                          // Open the database
                          Class.forName(DRIVER).newInstance();
                          connection = DriverManager.getConnection(URL);
                          //Verify nicno
                          sql="SELECT * FROM Candidate where nicNo= ?";          
                   pstmt1 = connection.prepareStatement(sql);
                   sql ="INSERT INTO Candidate                               
                   VALUES(?,?,?,?,?,?)";
                   pstmt2 = connection.prepareStatement(sql);
             catch (Exception ex) {
                System.err.println(ex.getMessage());
         public boolean verifyNicNo(){
              boolean nic_no_select_ok = false;
              String nic="xxxx";
              initConnection();
                    try {
                       pstmt1.setString(1, nicno);
                   ResultSet rs1 = pstmt1.executeQuery();               
                                if(rs1.next()){
                        nic=rs1.getString("nicNo");
                               if(nic=="xxxx")
                        nic_no_select_ok = true;
                   } else{
                        nic_no_select_ok = false;     
                   rs1.close();
                           pstmt1.close();
                    catch (Exception ex) {
                      System.err.println(ex.getMessage());
              return nic_no_select_ok;
         public boolean addInter(){
              boolean add_inter_ok = false;
              try{
                      //assign values for the object
                   pstmt2.setString(1, nicno);   //      ERROR(java:652)
                   pstmt2.setString(2, title);
                   pstmt2.setString(3, firstName);
                   pstmt2.setString(4, civilStatus);
                   pstmt2.setString(5, tele);     
                   pstmt2.setString(6, eduQua);
                   if(pstmt2.executeUpdate()==1) add_inter_ok=true;
                   pstmt2.close();  
                  catch(SQLException e2){
                           System.err.println(e2.getMessage());
              return add_inter_ok;
    IntreviewForm.jsp (used to submit data into the db)
    <form method="POST" action="InterviewCtrl.jsp">
         //codes
    <input type="text" name="nicno" size="14" style="border:1px solid #0000FF;           
    color: #FF0000; font-weight: bold">
    <input type="text" name="title" size="14" style="border:1px solid #0000FF;           
    color: #FF0000; font-weight: bold">
    <input type="text" name="firstName" size="14" style="border:1px solid #0000FF;           
    color: #FF0000; font-weight: bold">
    <input type="text" name="civilStatus" size="14" style="border:1px solid #0000FF;           
    color: #FF0000; font-weight: bold">
    <input type="text" name="tele" size="14" style="border:1px solid #0000FF;           
    color: #FF0000; font-weight: bold">
    <input type="text" name="eduQua" size="14" style="border:1px solid #0000FF;           
    color: #FF0000; font-weight: bold">
    </form>.......................................................................
    Here is the "InterviewCtrl.jsp"
    <%@ page language="java" import="java.util.*"%>
    <%@ page import="hrm.CandidateMgr" %>
    <%@ page session="false" %>
    <jsp:useBean id="candidate" class="hrm.CandidateMgr" scope="request"/>
    <jsp:setProperty name="candidate" property="*"/>
    <%-- Execute the addInter() method on the bean and check if it is true or false.-- %>
    <%-- if it's true then display a confirmation message "InterConfirm.html" --%>
    <%-- else display InterError.html --%>
    <%
    String nextPage ="MainForm.jsp";
    if(candidate.addInter()){
         nextPage="InterConfirm.html";
         }else{
         nextPage="InterError.html";
    %>
    <jsp:forward page="<%=nextPage%>"/>.......................................................................
    Here is the error:(I mark it in my bean also)
    root cause
    java.lang.NullPointerException
         at hrm.CandidateMgr.addInter(CandidateMgr.java:652)
         at org.apache.jsp.InterviewCtrl_jsp._jspService(InterviewCtrl_jsp.java:66)
         at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:137)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
    I use JSP 1.2 , mySQL with TOMCAT. My table name is Candidate.It's fields are as follows:
    Candidate(nicNo,title,fName,civilStatus,tele,eduQua)
    Can anybody tell me whats going on, help me to solve this please.
    Thanks.

    you should try something different, since you are mixing up a lot of code.
    1) create a separate method just to connect and disconnect from a database;
    2) use the value entered and see if you can find a record in the table. If the record is there, then you should re-direct your jsp to a jspError, else goes to another form.
    the code below give you some idea
          if( sAction.equals("Add1") ) {
            sITEM = request.getParameter("parameter1");
            try {
              itemmaster.connect();
              itemmaster.viewitemmaster( sITEM );
              rs = itemmaster.getRs();
              if( rs.next() ) {
                sURL = "additemmaster1.jsp";
              else {
                request.setAttribute( "asITEM", sITEM );
                sURL = "additemmaster2.jsp";
            finally {
              rs.close();
              itemmaster.disconnect();
          }

  • Insert XML data into oracle table

    I want to insert xml data returned by the VB code into oracle table.
    As a prequisite I have installed the XDK capabilities for Oracle by installing JServer & running
    SQL scripts catxsu.sql,xmlparserv2.jar,load.sql to load the XMLSQL Utility (DBMS_XMLQuery) into the database.
    I have also granted following privileges to the user.
    Grant users access to XMLParser and XMLDom packages:
         grant execute on xmldom to public;
         grant execute on xmlparser to public;
         create public synonym xmldom for sys.xmldom;
         create public synonym xmlparser for sys.xmlparser;
    But still i am not able to create procedure which will accept input parameter as an XML document coming from front end which in turn will insert that record into simple oracle table . I am using Oracle 8.1.7
    Thanks in advance

    Would you specify the database version?
    Since DBMS_XMLSave requires DOM, you normally need to divide the huge XML before insertion.

  • How to insert static data into ADF Table

    Hi
    I have a requirement , As a part of development of my prototype. I have to populate some static data into table.How to achieve this without using any java is there any way to simply hard code the values into adf table rows.?
    Thanks in advance

    http://www.oracle.com/technology/products/jdev/htdocs/partners/addins/exchange/jsf/doc/devguide/table.html
    http://otndnld.oracle.co.jp/document/products/as10g/101300/B25221_03/web.1013/b25386/web_MasterDetail006.htm
    http://andrejusb.blogspot.com/2006/12/displaying-all-columns-in-adf-table.html

Maybe you are looking for