IN (:userlist) 扩展为多个 OR 语句。
查询优化器忽略 OR 行/子句。
如果数据库是 Oracle,请执行以下操作:
CREATE TABLE userListTable
(
sessionId NUMBER(9),
user NUMBER(9)
);
CREATE INDEX userListTableMulti1 ON userListTable(sessionId,user);
...
CREATE OR REPLACE FUNCTION fn_getUserList(parmUserList VARCHAR2)
RETURN NUMBER DETERMINISTIC
varUser NUMBER(9);
varSessionId NUMBER(9);
BEGIN
varSessionId := sys_context('USERENV','SESSIONID');
-- You have to work on a VARCHAR2TOLIST() function
FOR varUser IN VARCHAR2TOLIST(parmUserList) LOOP
INSERT INTO userListTable(sessionId,user)
VALUES(varSessionId, varUser)
END LOOP;
INSERT INTO resultsTable
SELECT
varSessionId as sessionId ,
a.* ,
b.*
FROM
(SELECT a.*
FROM a
INNER JOIN userListTable
ON a.user = userListTable.user AND
userListTable.sessionId = varSessionId) a
LEFT OUTER JOIN (SELECT b.*
FROM b
INNER JOIN userListTable
ON b.user = userListTable.user AND
userListTable.sessionId = varSessionId) b
ON b.key = a.fk_to_b;
RETURN varSessionId;
END;
/
...
// C Client side
int varSessionId;
char* parmUserList;
char* sqlStr;
...
sqlStr = (char*)malloc( strlen(parmUserList) + 17 ) ;
sprintf(sqlStr,"fn_getUserList(%s)", parmUserList);
// EXEC_SQL_FUNC_C_MACRO
// EXEC_SQL_RETURN_QUERY_RESULTS_C_MACRO
// EXEC_SQL_C_MACRO
// are all based on the database API C libraries
// Run the function for this session
varSessionId = EXEC_SQL_FUNC_C_MACRO(sqlStr);
free(sqlStr);
// Get the results
sqlStr = (char*)malloc(128);
sprintf(
sqlStr,
"SELECT * "
"FROM resultsTable "
"WHERE sessionId=%s",
varSessionId);
EXEC_SQL_RETURN_QUERY_RESULTS_C_MACRO(sqlStr);
free(sqlStr);
...
// Clean up the resultsTable for this session
sqlStr = (char*)malloc(128);
sprintf(
sqlStr,
"DELETE "
"FROM resultsTable "
"WHERE sessionId=%s",
varSessionId);
EXEC_SQL_C_MACRO(sqlStr);
free(sqlStr);
// Clean up the userListTable for this session
sqlStr = (char*)malloc(128);
sprintf(
sqlStr,
"DELETE "
"FROM userListTable "
"WHERE sessionId=%s",
varSessionId);
EXEC_SQL_C_MACRO(sqlStr);
free(sqlStr);