Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

SQL Anywhere Tips and Tricks

2,407 views

Published on

This lecture will include examples of how to use some of SQL Anywhere's lesser known but incredibly powerful functionality to deliver applications that can do more for your customers. Features discussed will include the SQL Anywhere http server, using external environments to call Java/C++/.Net libraries, materialized views, and more.

Published in: Technology
  • Be the first to comment

SQL Anywhere Tips and Tricks

  1. 1. (c) 2015 Independent SAP TechnicalAnnual Conference, 2015 SQL AnywhereTipsandTricks Jason Hinsperger Product Manager, SAP
  2. 2. (c) 2015 Independent SAP TechnicalAnnual Conference, 2015 SQL AnywhereTipsandTricks Autocommit C External Environments Dedicated Tasks sp_list_directory and friends Variables in USING and AT clauses
  3. 3. (c) 2015 Independent SAP TechnicalAnnual Conference, 2015 AUTOCOMMIT
  4. 4. (c) 2015 Independent SAP TechnicalAnnual Conference, 2015 Autocommit Autocommit Off Chained On Autocommit On Chained Off IT DEPENDS! For TDS connections Autocommit on/off is effectively equivalent to Chained off/on  jConnect, Open Client For other connections, Autocommit and Chained are completely different  ODBC, SQLA JDBC, .NET, … Chained mode controls whether the server does a commit after each statement
  5. 5. (c) 2015 Independent SAP TechnicalAnnual Conference, 2015 Autocommit VSChained(ODBC Example)  CREATE PROCEDURE p( IN id INT, OUT numrows INT )  BEGIN  SAVEPOINT sv;  INSERT INTO tab2 SELECT * FROM tab1 WHERE tab1.tabid < id;  SELECT count(*) INTO numrows FROM tab2;  ROLLBACK TO SAVEPOINT sv;  END If Autocommit is ON (driver default) and Chained is OFF (database default), then executing the above procedure via ODBC does not change the contents of tab2 If Autocommit is either ON or OFF and Chained is ON, then executing the above procedure via ODBC does change the contents of tab2 AND will generate an error
  6. 6. (c) 2015 Independent SAP TechnicalAnnual Conference, 2015 TurningAutocommit Off So when we say “turn autocommit off” we actually mean make the API call on the client  ODBC: SQLSetConnectAttr( … SQL_ATTR_AUTOCOMMIT … )  JDBC: Connection.setAutoCommit( false ) (regardless of whether you are using jConnect or SQLA JDBC)  … AND avoid messing with chained mode
  7. 7. (c) 2015 Independent SAP TechnicalAnnual Conference, 2015 C EXTERNAL ENVIRONMENTS
  8. 8. (c) 2015 Independent SAP TechnicalAnnual Conference, 2015 C External EnvironmentsvsIn Process External StoredProcedures SQL Anywhere has had the ability to load user provided dlls and shared objects for a long time More recently these dlls and shared objects can now be loaded and executed outside of the server using C External Environments Loading these dlls and shared objects outside of the server provides:  Improved server stability  Greater security of data  Mixing and matching of server and dll/shared object bitness  The ability to return result sets from the external stored procedure  The ability to make server side calls via ODBC or ESQL  Easier debugging of external dll/shared object
  9. 9. (c) 2015 Independent SAP TechnicalAnnual Conference, 2015 C External EnvironmentsvsIn Process External StoredProcedures Changing an external stored procedure from in process to out of process is as simple as adding a LANGUAGE clause  CREATE PROCEDURE myextproc(…) EXTERNAL NAME ‘MyExternalProc@myprocs.dll’  CREATE PROCEDURE myextproc(…) EXTERNAL NAME ‘MyExternalProc@myprocs.dll’ LANGUAGE C_ODBC64 EXTERNAL NAME clause can contain both Unix and Windows definitions – exactly the same as the in process EXTERNAL NAME clause
  10. 10. (c) 2015 Independent SAP TechnicalAnnual Conference, 2015 C External Environments- DEMO
  11. 11. (c) 2015 Independent SAP TechnicalAnnual Conference, 2015 C External EnvironmentsvsIn-Process External StoredProcedures Things to take into consideration when deciding to load dlls and shared object in C External Environment:  Time required to make a call out of process is significantly greater than making an in process call  Machine resource utilization is higher due to separate process per connection  Sharing of data across connections is not allowed
  12. 12. (c) 2015 Independent SAP TechnicalAnnual Conference, 2015 DEDICATED TASKS
  13. 13. (c) 2015 Independent SAP TechnicalAnnual Conference, 2015 Scenario I have a large number of users who hammer away at my server throughout the day Of these users, I have a couple of admin users who get in earlier than the rest and who must be given priority when they make server requests The response time for my admin users decreases significantly when the rest of the users get in and start making constant server requests How can I fix it so that my admin users are always given priority over the other users I am not as concerned about the response time for requests from my non-admin users
  14. 14. (c) 2015 Independent SAP TechnicalAnnual Conference, 2015 Dedicatedtasks One way to solve this problem is to assigned dedicated server tasks to the admin users This is done using the dedicated_task database option The admin users could be assigned a login procedure that automatically sets the dedicated_task option to on for that connection Or the admin users could be given permission to set and unset the option on the fly as needed
  15. 15. (c) 2015 Independent SAP TechnicalAnnual Conference, 2015 DedicatedTasks- DEMO
  16. 16. (c) 2015 Independent SAP TechnicalAnnual Conference, 2015 Dedicatedtasks Note that dedicating a server task to a connection reduces the number of server tasks available for servicing requests from other connections This task cannot be used by the server for another connection EVEN IF the connection that the task is dedicated to is not currently executing a request in the server This feature should therefore be used with caution to avoid starvation of requests However for those unique situations where a request absolutely must make it into the server; the dedicated_task option can be very useful
  17. 17. (c) 2015 Independent SAP TechnicalAnnual Conference, 2015 SP_LIST_DIRECTORY AND FRIENDS
  18. 18. (c) 2015 Independent SAP TechnicalAnnual Conference, 2015 Preamble: DirectoryAccessServers sp_list_directory() and friends can be used as a light weight replacement for directory access servers Directory access servers allow applications to create proxy tables for “querying” and manipulating directories on the server machine  CREATE SERVER … CLASS ‘directory’ USING ‘root=…;subdirs=…’ Some people find that directory access servers are cumbersome and too powerful for what they really want to do
  19. 19. (c) 2015 Independent SAP TechnicalAnnual Conference, 2015 sp_list_directoryandfriends To provide an alternate approach to querying and managing directories on server machines, we added a set of file and directory stored procedures      Combined with xp_read_file() and xp_write_file(), this full set of stored procedures can provide the same functionality as directory access servers without requiring the creation of remote servers and extern logins  Directory procedures File procedures dbo.sp_list_directory() dbo.sp_copy_directory() dbo.sp_copy_file() dbo.sp_move_directory() dbo.sp_move_file() dbo.sp_delete_directory() dbo.sp_delete_file()
  20. 20. (c) 2015 Independent SAP TechnicalAnnual Conference, 2015 sp_list_directoryandfriends- DEMO
  21. 21. (c) 2015 Independent SAP TechnicalAnnual Conference, 2015 sp_list_directoryandfriends– final thoughts In some ways, sp_list_directory() and friends are more efficient than directory access servers They are also easier to use and do not require any setup Their use can also be easily restricted via system privileges and secure features However, directory access servers have certain optimizations that do not currently exist with sp_list_directory() and friends  Select … from dirtab where file_name=‘…’  Select … from dirtab where left(file_name, …) = ‘…’ But if all you want to do is list the contents of a directory, fetch some files or do some directory or file administration, sp_list_directory() and friends will usually provide the better alternative 
  22. 22. (c) 2015 Independent SAP TechnicalAnnual Conference, 2015 VARIABLESIN ‘USING’ AND ‘AT’ CLAUSES
  23. 23. (c) 2015 Independent SAP TechnicalAnnual Conference, 2015 Preamble: Driver = SQL AnywhereNative When creating DSN-less remote servers to another SQL Anywhere server, use Driver=SQL Anywhere Native instead of Driver=SQL Anywhere 16  eg. CREATE SERVER rem CLASS ‘saodbc’ USING ‘Driver=SQL Anywhere Native;host=…’  “Driver=SQL AnywhereNative” works on all platforms that support RDA  Database can be moved from machine to machine and across platforms without requiring changes to the remote server definition  ODBC Driver must be available on the machine but does not need to be registered  Server bypasses ODBC Driver Manager and loads the ODBC Driver directly  Bypassing ODBC Driver Manager gives a moderate (10%) perf. improvement  If multiple copies of the ODBC Driver are installed, then the server will pick the first one it finds (usually that is the one that was installed with that version of the server)
  24. 24. (c) 2015 Independent SAP TechnicalAnnual Conference, 2015 Preamble: ExternLogins 1. When making a remote server connection, SQLA will first attempt to use EXTERNLOGIN credentials of the current executing user 2. If there are no EXTERNLOGIN credentials of the current executing user, then SQLA will fail if the current executing user is not the the current logged in user 3. If the current executing user is the same as the current logged in user and there are no EXTERNLOGIN credentials for the current logged in user, then SQLA will attempt to use the local credentials of the current logged in user These credentials are appended to the connection string that is sent to the underlying driver; hence any userid or password values in either the DSN or the USING clause will get overridden  CREATE SERVER … USING ‘Driver=SQL Anywhere Native; …;uid=…;pwd=…’ will not work because driver sees two sets of uid=
  25. 25. (c) 2015 Independent SAP TechnicalAnnual Conference, 2015 Preamble: ExternLogins Trick: To fool SQL Anywhere into NOT appending uid= and pwd= and instead using the uid= and pwd= in either the USING clause or the DSN; create a dummy EXTERN LOGIN with no remote login  CREATE EXTERNLOGIN myuser TO myserver  Creates an externlogin for myuser to server myserver with no remote login/password  SQLA will see this externlogin and assume myuser has no remote userid/password for server myserver  As a result, SQLA will not append any uid= or pwd= when making a connection to myserver IF the current effective user is myuser  This allows the driver to use the uid/pwd that is in the USING clause or the DSN
  26. 26. (c) 2015 Independent SAP TechnicalAnnual Conference, 2015 Preamble: USING andAT Clauses CREATE SERVER … CLASS ‘…’ USING ‘…’ CREATE EXISTING TABLE … AT ‘…’ CREATE PROCEDURE … AT ‘…’ CREATE FUNCTION … AT ‘..’ AT clauses are sometimes also called LOCATION clauses  Trick: CREATE TABLE table-name ( column-list ) AT ‘…’ will create both the proxy table and the remote table at the same time
  27. 27. (c) 2015 Independent SAP TechnicalAnnual Conference, 2015 Variablesin USING andAT Clauses  The USING and AT clauses can contain substitution variables which get evaluated at run time  These variables take the form {variable-name} and can appear anywhere  Variables in the USING and AT clause can be used to redefine or change the remote server definition or proxy table/procedure location on the fly  This can be extremely useful if you have multiple remote servers, remote tables or remote procedures that you need to manage but do not want to have to set up individual remote server definitions, proxy table and/or proxy procedure definitions  Variables in USING and AT clauses can be used with any remote server and is not restricted to SQL Anywhere back ends
  28. 28. (c) 2015 Independent SAP TechnicalAnnual Conference, 2015 Variablesin USING andAT Clauses- DEMO
  29. 29. (c) 2015 Independent SAP TechnicalAnnual Conference, 2015 Final Thoughts The features presented here are only some of the lesser known but extremely useful features in SQL Anywhere A few other such features are:  Scripting external environments (PERL and PHP)  TRY … CATCH  dbo.sp_forward_to_remote_server()  Sandboxing  MERGE  Secure features  … All of the features presented here and many more are available today
  30. 30. (c) 2015 Independent SAP TechnicalAnnual Conference, 2015 Final Thoughts Let us know about your favorite SQL Anywhere feature and how you use it  http://scn.sap.com/community/sql-anywhere  Get involved in the SQL Anywhere forum:  http://sqlanywhere-forum.sap.com
  31. 31. (c) 2015 Independent SAP TechnicalAnnual Conference, 2015 Thank you Jason Hinsperger jason.hinsperger@sap.com

×