Or are the little hats just treated as text characters because it's a LOCATE function and not a SIMILAR TO or REGEXP or REGEXP_SUBSTR ?

snippet from a WHERE clause....

AND ( LOCATE ( ''^xxxyy^'' , ''^'' + table.column + ''^'' ) > 0 OR ''^xxxyy^'' ='''' )

How am I meant to interpret the second part of it? (The ''^xxxyy^'' ='''' part)

asked 24 May '11, 18:31

Margaret%20Kammermayer's gravatar image

Margaret Kam...
3316819
accept rate: 50%


The predicates that you have given are not regular expressions by merely a normal text locate search ( LOCATE ( ''^xxxyy^'' , ''^'' + table.column + ''^'' ) > 0 ) and text string equality test ( ''^xxxyy^'' ='''' ).

Note that what appears to be double quotes are in fact double single quotes. This leads me to believe that you have copied your code snippet from a piece of code that is composing an expression that will be used in an EXECUTE IMMEDIATE !

If we take the code snippet at its face value (and ignoring the fact in the previous paragraph), the '^xxxyy^' = '' predicate will always evaluate to FALSE so it is of little use in the predicate evaluation.... BUT ...

The LOCATE( ''^xxxyy^'' , ''^'' + table.column + ''^'' ) > 0 predicate makes me believe that the xxxyy string is going to be replaced with a "real value" before the expression is evaluated by the EXECUTE IMMEDIATE. Assuming this is true, then it would imply that the LOCATE expression is checking to see if the named column matches the replacement string.

It may be helpful if you were to either provide more context or show us the full piece of code.

permanent link

answered 24 May '11, 19:30

Mark%20Culp's gravatar image

Mark Culp
24.9k10141297
accept rate: 41%

The carets (^) are not treated as special characters in the LOCATE() function. If table.column does not have any carets in the column values, then the expression you list is almost* equivalent to "table.column ='xxxyy'" except that an index will not be used to answer this query.

The second part of the condition evaluates to FALSE (and the OR will be simplified to merely the first condition).

Without knowing the structure of the data it is hard to know why the search condition is coded this way. If the constant literal '^xxxyy^' is being generated by software, then the second condition will "turn off" the search condition if an empty string is passed in.

In the above, I said that the LOCATE() condition is almost equivalent to "table.column='xxxyy'". Depending on the character collation used, LOCATE() may give different answers for some comparisons:

select unistr('\\u00DF') S, N'ss' ss
    , if S=ss then 'eq' else 'neq' endif iseq
    , locate( ss, S ) loc

The above query returns: ('ß','ss','eq',0)

The LOCATE() function uses character-by-character comparisons (as does LIKE and some other functions), while the equality operator uses string-by-string comparisons.

permanent link

answered 24 May '11, 19:34

Ivan%20T.%20Bowman's gravatar image

Ivan T. Bowman
2.8k22732
accept rate: 39%

edited 24 May '11, 19:34

2

WOW! Many thanks to both of you!

The snippet comes from a query that showed up in a client's database trace as a time-consuming one. The SQL comes from a PowerBuilder application, so it could be created in a DataWindow or it could be explicitly coded, and I suppose it could be followed by an EXECUTE IMMEDIATE as Mark suggests. I don't have access to the application code, so I can't say for sure.

But since no index would be used for this part of the query, and several other search conditions in the same query use columns that have no index on them either, I'm thinking that a portion of the performance problem might be right here.

(24 May '11, 20:12) Margaret Kam...
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×36

question asked: 24 May '11, 18:31

question was seen: 4,036 times

last updated: 24 May '11, 20:12