I am researching whether it is possible to use dynamic hints in Oracle. We have system for a lot of customers with different volumes of data, so we want to personalize queries for each of them.
I would like to do this:
PROCEDURE PROCESS_STEP_1 (p_cust_id NUMBER)
IS
v_hint_value NUMBER;
BEGIN
-- Select correct value from param table
SELECT value INTO v_hint_value
FROM param_table
WHERE cust_id = p_cust_id
AND process_name = 'PROCESS_STEP_1';
INSERT INTO result_table
SELECT /*+ PARALLEL v_hint_value */ * FROM etc.
END;
I know that it's possible to do this is via dynamic queries. We had a lot of them and had to abandon them due to bad readability and other issues (those SQLs are rather complex).
Do you have any idea how something like this could be implemented? Thanks!
OK, I will start with a complete disavowal of this approach. I write a lot of high-performing SQL applications and very rarely use hints. The fact that you seem to be talking about a massively hinted application where the hints vary by installation is a big red flag. I recommend you try to remove the hints and address what may be underlying application design issues.
That all said, Oracle has a SQL translation feature. It is intended to let you translate SQLs from applications written for non-Oracle databases, but it can serve your purpose too. What you want to do is use this feature to "translate" hints in any inbound SQL statement before Oracle parses it.
Here is sample code to illustrate the concept and get you started. Again -- I am not recommending this, but SO is a place for answers, not lectures, so here it is...
Step 1 -- Make sure you have the privileges to do this
Create a table to store hint translations
The idea here is that you embed hints in your application like "/+HINT12345/ and that you would populate this table differently at each customer site. You can obviously get more sophisticated than this.
Create a PL/SQL Package to perform the hint translation
You must use the procedure names and parameters as given. They are required by the
DBMS_SQL_TRANSLATORpackage we'll be using.Tell Oracle to start using our translator