Архив Тэгов: ссылки

Oracle SQL and PL/SQL

A problem with Bind Variable Peeking in DSS systems turns out to be a confederate

written by Victor Varlamov, OCP at 07-JUL-2017

There is a Reporting system, where a lot (more than 100) of complex long running queries are launched by Reporting Engine, triggered by different business events. Queries are executed with quite different input parameters (list of customers) and data ranges (daily, weekly, monthly). Because of skewed data in tables, one report can return anywhere from 1 row to 1 million rows depending on the input parameters of the report (different clients have different quantities of rows in fact tables). Every report is implemented as a PL/SQL package with a main function that accepts input parameters, prepares some additional data, and then opens a static cursor with PL/SQL variables as parameters, and finally returns the opened cursor. The CURSOR_SHARING parameter is set to FORCE in the DB server. In such scenarios, SQL can lead to poor performance if the optimizer re-uses unsuitable execution plans that otherwise would not be used if a hard parse was forced using literals. Bind variable peeking can cause a suboptimal plan.

In the book Expert Oracle Practices, Alex Gorbachev relates a story — told to him by Tom Kyte — in which the query plan would change on Monday mornings if it was raining. It’s a true story.

“According to the end-users’ observations, if it was raining heavily Monday morning, then database performance was terrible. Any other day of the week, or on Mondays without rain, there were no problems. Talking to the DBA responsible for the system, Tom found that the performance problems continued until the DBA restarted the database, at which point performance went back to normal. That was the workaround: Rainy Mondays = Reboots.”

It is a real case, and this case was solved without any mysteries and magic, just good knowledge of how to Oracle DB works. I’ll show the solution at the end of the article.

Here is a simple example of how bind variable peeking works:

читать далее »
Oracle Beer Day SQL and PL/SQL

Песня про 99 бутылок пива

Поистине замечательный ресурс для программиста любого ранга и языка — www.99-bottles-of-beer.net, в котором более чем 1500 примеров реализации вывода песни про 99 бутылок пива на самых разных языках программирования и с использованием различных приемов. Есть и варианты на разных диалектах SQL. В свободное от работы время я соорудил и свой скрипт, не совсем верно передав текст песни, но идея, думаю, будет понятна.

SELECT CASE MOD (400 - LEVEL, 4)
          WHEN 3
          THEN
             TRUNC ((400 - LEVEL) / 4) || ' bottles of beer on the wall'
          WHEN 2
          THEN
             TRUNC ((400 - LEVEL) / 4) || ' bottles'
          WHEN 1
          THEN
             'Drink 1 bottle...'
          ELSE
             CASE
                WHEN LEVEL > 396
                THEN
                   TRUNC ((400 - LEVEL) / 4) - 1 || ' bottles left'
                ELSE
                   'No more bottles on the wall'
             END
       END
  FROM DUAL CONNECT BY LEVEL >= 396

Результат:

99 bottles of beer on the wall
99 bottles
Drink 1 bottle…
98 bottles left
98 bottles of beer on the wall
98 bottles
Drink 1 bottle…

Drink 1 bottle…
1 bottles left
1 bottles of beer on the wall
1 bottles
Drink 1 bottle…
No more bottles on the wall

Oracle

Интернет-журнал ФОРС

Полезная ссылка на полезное чтиво об Оракле — Интернет-журнал ФОРС. Тот случай, когда конкуренты делают общее дело!