简体   繁体   English

使用concat的Oracle查询问题|| 算子

[英]Oracle query issue using concat || operator

I am trying to query a string as follows, 我正在尝试如下查询字符串,

Currently my Oracle table has around 60 columns. 目前,我的Oracle表有大约60列。 So to query a string for against all columns will be as follows, 因此,针对所有列查询字符串如下:

Working query: 工作查询:

select * from <table_name> where (col1||col2||col3...||col60) like '%string%'

Not working query: 无法运作的查询:

i have added one more query to get all columns dynamically as below, 我又添加了一个查询以动态获取所有列,如下所示,

select * from <table_name> where (select LISTAGG(COLUMN_NAME, '||') WITHIN GROUP (ORDER BY COLUMN_NAME) from all_tab_columns where TABLE_NAME = '<table_name>' and OWNER = '<owner_name>' and DATA_TYPE != 'CLOB') like '%string%'

sub query : 子查询:

There is no issue with the below subquery. 以下子查询没有问题。

select LISTAGG(COLUMN_NAME, '||') WITHIN GROUP (ORDER BY COLUMN_NAME) from all_tab_columns where TABLE_NAME = '<table_name>' and OWNER = '<owner_name>' and DATA_TYPE != 'CLOB'

Where the subquery is returning output like in the working query. 子查询正在返回输出的地方,如工作查询中的那样。

I am thinking that the oracle taking the "||" 我认为甲骨文采取“ ||” concat operator only when it is hardcoding it. concat运算符仅在对其进行硬编码时才使用。

Could you please help me on that? 你能帮我吗?

Here is how I would solve your problem in a way that would scale. 这是我将以可扩展的方式解决您的问题的方法。

CREATE OR REPLACE VIEW COLUMN_SEARCH AS

SELECT ID, COL1 AS COL
FROM TABLENAME

UNION ALL

SELECT ID, COL2 AS COL
FROM TABLENAME

UNION ALL

SELECT ID, COL3 AS COL
FROM TABLENAME

UNION ALL

SELECT ID, COL4 AS COL
FROM TABLENAME

UNION ALL

--- 

SELECT ID, COLN AS COL
FROM TABLENAME

Now with this view your query is just 现在有了这个视图,您的查询就是

SELECT TABLENAME.*
FROM TABLENAME
JOIN COLUMN_SEARCH ON TABLENAME.ID = COLUMN_SEARCH.ID
WHERE COLUMN_SEARCH.COL = 'string'

If you need this to be dynamic then just update view everytime there is a new column added. 如果您需要动态显示,则每次添加新列时都只需更新视图。

If it is running slow then just make an index on ID, COL in the view. 如果运行缓慢,则只需在视图中的ID,COL上建立索引。


Update based on comments 根据评论更新

I want to add a some comments about using dynamic SQL to solve this problem. 我想添加一些有关使用动态SQL解决此问题的注释。 You can use dynamic SQL to solve this problem but it would be a bad idea. 您可以使用动态SQL解决此问题,但这不是一个好主意。 ( For those that don't know -- creating the query dynamically from meta data about the columns everytime you run the query would be using dynamic SQL ) 对于那些不知道的人,每次运行查询时,从有关列的元数据动态创建查询将使用动态SQL

Here is why I don't recommend it: 这就是为什么我不推荐的原因:

  1. Dynamic SQL is really hard to maintain -- it is (by definition) more complicated than non dynamic SQL and it never looks very much the query you are really trying to run. 动态SQL确实很难维护 -从定义上说,它比非动态SQL更复杂,并且它看起来从没有真正要运行的查询。 While it seems like it would make maintenance easier "Everytime I add a column it just automatically adds in in - bim-bamb-boom!" 看起来这将使维护更加容易“每次我添加一列,它只会自动添加到-bim-bamb-boom!” In fact, I've found everytime you add a column it is better to make a choice "Do I want to add this in." 实际上,我发现每次添加列时,最好选择“是否要添加它”。 Sometimes it helps to automatically add it but many times it will break the system. 有时它有助于自动添加它,但是很多时候它将破坏系统。

  2. Dynamic SQL breaks optimization -- SQL platforms have decades of optimization built into their compilers, they do an amazing job. 动态SQL破坏了优化 -SQL平台在其编译器中内置了数十年的优化,它们做得非常出色。 Because dynamic SQL can't be per-compiled and cached you lose a lot on performance in addition to the time it takes to create the dynamic SQL. 因为无法对动态SQL进行每次编译和缓存,所以除了创建动态SQL所需的时间外,还会损失很多性能。

  3. Dynamic SQL can't easily be tweaked and tuned -- A lot of what a DBA does is look at how data models are being used and then add indexes, partitions, materialized queries and other tuning to make the system run quickly. 动态SQL不能轻易地进行调整和调优 -DBA所做的很多事情就是看数据模型的使用方式,然后添加索引,分区,物化查询和其他调整以使系统快速运行。 It is much harder to do this kind of tuning on dynamic SQL (but not impossible). 在动态SQL上进行这种调优要困难得多(但并非没有可能)。

  4. Dynamic SQL is a bad code smell -- This is more subtle and may not apply to this particular question but needs to be said -- if you are using dynamic sql it is probably an indication you did something wrong with your data model or you are using the wrong tool. 动态SQL是一种不好的代码味道 -这比较隐蔽,可能不适用于此特定问题,但需要说-如果使用动态SQL,则可能表明您对数据模型做错了使用错误的工具。 There are a number of "nosql" systems out there that might be better suited for solving your particular data issue. 有许多“ nosql”系统可能更适合解决您的特定数据问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM