reliable detect number of occurrences (necessary lock table or?)

Hi all

I had a problem with the detection of duplicate messages. Customer database deal with files and messages and create a hash value that is passed to the database. The database must return the number of occurrences of this hash value in (for example) the last 14 days.
create table HashHistory ( ID NUMBER, 
MESSAGEHASH VARCHAR2 (20),
TS TIMESTAMP);

create sequence SomeSequence;

insert into HashHistory values (SomeSequence.nextval,'first hash', systimestamp); 
insert into HashHistory values (SomeSequence.nextval,'second hash', systimestamp); 

create or replace procedure DuplDetection  (p_HashIn varchar2, 
                                     p_occurences OUT number) AS
l_timestamp timestamp default systimestamp;                           
begin
 
  -- possible exclusive table lock here... lock table HashHistory in exclusive mode;

  insert into HashHistory values (SomeSequence.nextval, p_HashIn, l_timestamp);

  select count (1) 
  into p_occurences
  FROM HashHistory
  where MESSAGEHASH = p_HashIn
  and   TS < l_timestamp
  and   TS > l_timestamp-14;

  commit; --to release the table lock if applicable
end;
When this procedure is called by two different machines at the same time with the same new hash value ("third hash"). A session should return 0 while the other must return 1 as the number of occurences... With the behavior of the default Oracle using row-level locking and run them in parallel the two sessions will not be able to see the other sessions of hash values, and both return 0 hits. Is an exclusive table lock my only option to respect this behavior or I can trust Oracle to handle this correctly?

I expect 10 ^ 6 axe every day and possible up to 10 or 20 customers running at the same time generate and check these hash values. What are the changes of these two sessions, return the same value without an exclusive table lock (as in this example)? What are the other settings in your opinion?

I'm on Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi

Calls to your DUPLDETECTION procedure must be serialized to same values of the parameter P_HASHIN, to fix the problem you describe.

Kordirko showed you a way how manually do this by using a second table with a primary key constraint.
He also mentioned the use of DBMS_LOCK to achieve this. That would be my preferred solution. First, create a procedure to acquire a lock in demand (a key dbms_lock):

create or replace procedure lock_assertion(p_lockname in varchar2) as
pl_id number(38);
pl_return number;
begin
  -- Get a unique hash for this lockname.
  pl_id := dbms_utility.get_hash_value(name      => p_lockname
                                      ,base      => 1
                                      ,hash_size => power(2,30));
  -- Request the application lock in the specified mode.
  pl_return := dbms_lock.request(id                => pl_id
                                ,lockmode          => dbms_lock.ssx_mode
                                ,timeout           => 999
                                ,release_on_commit => true);
  if pl_return not in (0,4)
  then
    raise_application_error(-20000,'Unable to acquire assertion-lock: '||
     p_lockname||' ('||to_char(pl_return)||').');
  end if;
  --
end;
/

Then call LOCK_ASSERTION before your insert command by using P_HASHIN as the input parameter value. Who must serialize calls to your procedure of dupldetection for values of p_hashin. When two clients now call the procedure with the same hash, one of these calls will wait you, for another call at the end (this is assuming that you'RE at the end of the dupldetection call, freeing the application blocking).

Tags: Database

Similar Questions

  • count the number of occurrence in the table with toplink

    Hello!

    There is no way to create a query with the expressionbuilder or... to count the number of occurences in my table?
    I don't want to use the query " select count (*) from table .

    Thank you

    Not sure about the question. You are looking to get the SQL "select count (*) table" to use the framework of expression TopLink or pull you that SQL already and want something else?

    If you are looking just to get the count of a table/class, you can use a ReportQuery:
    ReportQuery rquery = new ReportQuery (ClassToQueryOn.class);
    rquery.addCount (); equivalent to count (*);

    session.executeQuery (rquery);

    You can use a report query to return data instead of objects and use the criteria for selection as a normal read request.

    Best regards
    Chris

  • SQL query: number of occurrence of cellData more thann a column of tabl

    I used table contains the sequence of lines.
    INSERT INTO employee VALUES (105, 'Srinath','vijay','Aeronautics', 27, 33000); 
    INSERT INTO employee VALUES (105, 'Kumble','Anil','Aeronautics', 27, 33000);
    INSERT INTO employee VALUES (105, 'Prabhakar','Manoj','Aeronautics', 27, 33000);
    INSERT INTO employee VALUES (105, 'Srinath','Jawagal','Aeronautics', 27, 33000);
    INSERT INTO employee VALUES (105, 'Jawagal','Srinath','Aeronautics', 27, 33000);
    INSERT INTO employee VALUES (105, 'Mishra','Anil','Aeronautics', 27, 33000);
    INSERT INTO employee VALUES (105, 'Kumble','Prabhakar','Aeronautics', 27, 33000);
    Select first_name separate firstName, count (1) on firstNameCount (partition name) of the employee;
    and I got the following result.
    (Srinath 2, Kumble 2, Prabhakar 1, Jawagal 1, Mishra 1)

    Now I want to examine the second and third column the two and want to number of occurrence of data in the table in these two columns
    (Srinath 3,Kumble 2,Prabhakar 2,Jawagal 2,Mishra 1,vijay 1,Anil 2,Manoj 1)
    As Srinath just 3 times, kumble 2 times, Prabhakar 2 Jawagal 2 times, Mishra 1 time, 1 time vijay times, Anil 2 times and Maury 1 times.

    What will be my sql query?

    Try this

    select name, count(name) over(partition by name) cnt
      from (select first_name from employee union all select last_name from employee)
    
  • How do I get the number of characters in the table

    Hi all

    I'm writing a query.

    Requirement is like this.

    You will need to obtain the total number of a character in a table.

    Here is an example of data.

    There are table T1 that has Column1, Column2, and Column3.

    Column1Column2Column3
    #*
    *&$
    (**
    *%!

    This is I want to get the total number of characters "*" in the table, regardless of the number of columns in this table.

    By the sample data above, I should get the count as '5'.

    I want to translate is not necessary:

    WITH got_columns_123 AS

    (

    SELECT column1 | Column2 | Column3 AS columns_123

    FROM t1

    )

    SELECT NVL (SUM ((columns_123) - NVL LENGTH (LENGTH (REPLACE (columns_123, ' *')), 0)), 0)

    OF got_columns_123;

  • How to get the number of occurrences per document

    Hello

    I have a table of documents (word, pdf, excel, txt) stored in the blob column. I want the SCORE to returns the number of occurrences in each document, so I use this piece of code:

    SELECT bdt.*, score_1, DBMS_CRYPTO SCORE (1). HASH(Archivo, 3) AS cod_hash

    OF crg_ctx_archivos_recibidos bdt

    WHERE CONTAINS

    (archivo,

    ' < query >

    < textquery = grammar 'SPANISH' lang = 'CONTEXT' > DEFINESCORE (paz, OCCURRENCE * 0.1) < / textquery >

    < score datatype = "INTEGER" algorithm = "COUNT" / >

    < / query > ', 1

    ) > 0

    ORDER BY PARTITION (1) DESC;

    However, I get a maximum score of 10, but I know that one of these documents have 490 games. How can I get the exact number of matches by document?

    Thanks in advance

    PD. I am using Oracle 11.2

    All the scores at the top inside to 100, so I don't think that you will be able to make the distinction between a document with 100 matches and the other with 490.

    Can you explain what you're trying to do with this?  There may be a way to use text search to find all docs with at least 100 shots, then their post-processing to order by the real number.

  • Need help finding the number of occurrences of a model.

    Hi all

    I need help to find the number of occurrences of a model in the column of the table data.

    Consider the examples of data - column of a row in a table:

    'S-S-S-A-S-S-P-S-S-B-S-A-P-S-S-C '.

    My requirement is:

    I should get the County of S that are immediately preceded by or P.

    for the above data, I should get are counted as 3 + 2 + 1 = 6 (S-S-S-A, S-S-P, S - A)

    The configuration data is stored as type VARCHAR2.

    Thanks in advance,
    Girish G

    Published by: Girish G on July 21, 2011 23:22

    I don't know that there is a better way, then this one:

    SQL> with dt as
      2  (select 'S-S-S-A-S-S-P-S-S-B-S-A-P-S-S-C' str from dual)
      3  SELECT SUM(Regexp_count(Regexp_substr(str, '(S\-?)+(A|P)+', 1,
      4                                     Regexp_count(str, '(S\-?)+(A|P)+') - (
      5                                                  LEVEL - 1 )), 'S')) len
      6  FROM   dt
      7  CONNECT BY LEVEL <= Regexp_count(str, '(S\-?)+(A|P)+')
      8  /
    
           LEN
    ----------
             6
    
  • Satellite P500 (PSPE8A-01R002) - number pad and num lock does not

    Number pad and num lock on my P500 PSPE8A-01R002 does not work after the upgrade to Windows 7.

    Any help will be appreciated.

    Hi AndrewF,

    Have you noticed the same problem on a preinstalled OS from Toshiba?
    As far as I know Satellite P500 are always delivered with Windows 7 OS provided with your P500?

    Anyway, in your case I would try updating the BIOS. You can get the latest version on the Toshiba site.

  • How to set Max number of rows in a table?

    Hello
    I'm working on a SCADA interface. In this application, there are different tables with vertical scroll bar.
    I prefer to make it visible only initialized lines (I've initialized 40 empty rows in a table with an array of strings). Since the window contains all 40 lines, but only 20 rows, I put the number of lines in the "table properties window" at 20; So I added a vertical scroll bar.

    Now, when a user, do scroll the scroll bar, it can check not only the initializated 40 lines, but any number of lines.
    There is a way to view only a limited number of lines?

    Thanks in advance!


  • How to index the occurrences in the table

    Hi, is there a way we can index every occurrence in the table?

    It seems that search that ID Array is once and I can't understand how do.

    Please notify

    Thanks in advance

    Clement

    You need only the index that corresponds to the item looking like this?

  • number of rows in the table

    Hi master,

    I have a requirement. During the passage of a table, I need to get the number of rows in this table. Sometimes, when I spend 2 or more table names, I need to get the same count of result of lines. I got the below function. but it shows all the rows in the table. I want pariticular tabels only. How I can I get it.

    Select table_name, num_rows from all_tables where table_name in ('EMP', 'Department');  I tried.  I need to a procedure or function. who will pass the name of the table as a parameter and 1 or more than the name of a table if I pass, I need to display information from table name and number of lines.

    CREATE OR REPLACE

    TYPE t1_obj AS OBJECT)

    table-name VARCHAR2 (30),

    CNT NUMBER

    )

    /

    CREATE OR REPLACE

    TYPE t1_obj_tbl AS THE t1_obj TABLE

    /

    CREATE OR REPLACE

    FUNCTION (f1)

    p_schema_name VARCHAR2

    )

    RETURN t1_obj_tbl

    PIPELINED

    IS

    v_retval t1_obj: = t1_obj (null, null);

    BEGIN

    FOR v_rec IN (SELECT table_name FROM dba_tables where owner = upper (p_schema_name) and nvl (iot_type, 'X')! = "IOT_OVERFLOW") LOOP

    v_retval.table_name: = v_rec.table_name;

    RUN IMMEDIATELY "SELECT COUNT (*) FROM" | p_schema_name | '.' || v_rec.table_name

    IN v_retval.cnt;

    PIPE ROW (v_retval);

    END LOOP;

    RETURN;

    END;

    Select * from table (f1 ('scott'));

    The above function returns information from the table in SCOTT scheama. I need only perticular table info.

    Please notify.

    AR.

    Hi Sven,

    Front end people ask me the requirement. They wanted a proc or function as I want.

    Please help me.

    Concerning

    AR

  • Essbase calc script to determine the number of occurrence

    Hello

    is it possible within a calc script to determine the number of occurrence of a value in the account dimension?

    I have an account called ranks which can contain any value between 1 and 10.  I would like to know how many times each value is repeated for a specific cost center.  It's in a database of planning BSO.

    Thanks in advance for your contributions...

    CL

    This isn't the typical calculation you would do in an OLAP architecture, but yes it is certainly possible.

    you will need to create 10 accounts such as occurrence_of_1, occurrence_of_2 etc.

    Then you increment them according to the value of the 5th year.

    Don't forget to put in your calculation!

  • How can I recover only odd or even number of records in the table?

    Hello

    How can I recover only odd or even number of records in the table?

    Thank you

    Hi IndiMinds,

    Alternatively, you can use in queries. :

    Odd:

    SELECT *.

    Of

    (SELECT rownum row_count, id, name FROM bigemp

    )

    WHERE mod (row_count, 2) = 1;

    Still:

    SELECT *.

    Of

    (SELECT rownum row_count, id, name FROM bigemp

    )

    WHERE mod (row_count, 2) = 0;

    Thank you

  • Adobe Premiere Elements 12: not compatible with EXECUTE-programm for my monitor detected an update is necessary.

    I got the message during the opening of a new project in Adobe Premiere elements 12: not compatible with EXECUTE-programm for my monitor detected an update is necessary. I tried several updates but windows7 does not detect an update. What can I do?

    Update your graphics driver.

  • How to get the total number of occurrences based on the value of a column.

    Hi all

    It is the first time I'll ask the question here on your forum, but since then followed several threads. I guess it's now my turn to ask a question. Anyway here's the thing, I have a query that should return to count the number of rows based on the value of HOUSING. Something like this:

    -----
    WIPDATAVALUE          SLOT             N            M
    1-2                   TRALTEST43S1     1            3
    1-2                   TRALTEST43S1     2            3
    3                     TRALTEST43S1     3            3
    4-6                   TRALTEST43S2     1            4
    4-6                   TRALTEST43S2     2            4
    4-6                   TRALTEST43S2     3            4
    7                     TRALTEST43S2     4            4
    -----

    As you can see above, on the TRALTEST43S1 of the SLOT, there are three occurrences, so M (Total number of occurrences) must be three and this column N he's counting. It is the same with the TRALTEST43S2 of the SLOT. It's the query I have so far:
    SELECT DISTINCT
    WIPDATAVALUE, SLOT
    , LEVEL AS n
    , m 
    FROM
    (
      SELECT
        WIPDATAVALUE
        , SLOT
        , (dulo - una) + 1 AS m 
      FROM
      (
        SELECT 
          WIPDATAVALUE
          , SLOT
          , CASE WHEN INSTR(wipdatavalue, '-') = 0 THEN wipdatavalue ELSE SUBSTR(wipdatavalue, 1, INSTR(wipdatavalue, '-')-1) END AS una
          , CASE WHEN INSTR(wipdatavalue, '-') = 0 THEN wipdatavalue ELSE SUBSTR(wipdatavalue, INSTR(wipdatavalue, '-') + 1) END AS dulo
        FROM trprinting
        WHERE (containername = :lotID OR SLOT= :lotID) AND WIPDATAVALUE LIKE :wip
      )
    ) CONNECT BY LEVEL <= m
    ORDER BY wipdatavalue;
    And it leads to something like this:
    -----
    WIPDATAVALUE          SLOT             N            M
    1-2                   TRALTEST43S1     1            2
    1-2                   TRALTEST43S1     2            2
    3                     TRALTEST43S1     1            1
    4-6                   TRALTEST43S2     1            3
    4-6                   TRALTEST43S2     2            3
    4-6                   TRALTEST43S2     3            3
    7                     TRALTEST43S2     1            1
    -----

    I think that my current query based results M and N on WIPDATAVALUE and not HOUSING that's why I get the wrong result. I also tried to use WITH instruction and it works well, but unfortunately, our system cannot accept the subquery factoring.

    I know that you guys will be of help because you are all awesome. Thank you all

    Published by: 1001275 on April 19, 2013 20:07

    Published by: 1001275 on April 19, 2013 20:18

    Hello

    1001275 wrote:
    Hi sb92075,

    You are right that it is available with this version. But our system doesn't put queries that use subquery factoring.

    What system are you talking about? If you really have something that prevents you from using all the features of Oacle, you should seriously think about fixing it.

    Any other ideas on how we can do this without help WITH clause?

    Yes; If a WITH clause is referenced that once, it can be re-written as a point of view online:

    SELECT       wipdatavalue
    ,       slot
    ,       ROW_NUMBER () OVER ( PARTITION BY  slot
                                 ORDER BY          low_number
                        )                    AS m
    ,       COUNT (*)     OVER ( PARTITION BY  slot )     AS n
    FROM       (     -- Begin in-line view (got_numbers)
                SELECT     wipdatavalue
              ,     slot
              ,     TO_NUMBER ( SUBSTR ( wipdatavalue
                                        , 1
                                   , INSTR ( wipdatavalue || '-'
                                               , '-'
                                        ) - 1
                                   )
                            )          AS low_number
              ,     TO_NUMBER ( SUBSTR ( wipdatavalue
                                        , 1 + INSTR ( wipdatavalue
                                                       , '-'
                                                )
                                   )
                            )          AS high_number
              FROM     trprinting
           )     -- End  in-line view got_numbers
    CONNECT BY     LEVEL               <= high_number + 1 - low_number
         AND     low_number          = PRIOR low_number
         AND     PRIOR SYS_GUID ()      IS NOT NULL
    ORDER BY  low_number
    ,            m
    ;
    
  • How to check the index, on the number of columns in the table is created

    How to check on the number of columns in the table the index has been created. ??

    OR

    How check index, on the number of columns in the table that it is been created?

    890306 wrote:
    How to check on the number of columns in the table the index has been created. ??

    >

    OR

    How check index, on the number of columns in the table that it is been created?

    query USER_IND_COLUMNS

    SQL> desc user_ind_columns
     Name                            Null?    Type
     ----------------------------------------- -------- ----------------------------
     INDEX_NAME                             VARCHAR2(30)
     TABLE_NAME                             VARCHAR2(30)
     COLUMN_NAME                             VARCHAR2(4000)
     COLUMN_POSITION                        NUMBER
     COLUMN_LENGTH                             NUMBER
     CHAR_LENGTH                             NUMBER
     DESCEND                             VARCHAR2(4)
    

    Handle: 890306
    Status level: Beginner
    Join date: October 8, 2011
    Messages total: 13
    Total Questions: 5 (5 open)

    Why all the question still pending?

    Published by: sb92075 on December 3, 2011 17:21

Maybe you are looking for