Mysql retrieve results of update query - mysql

I am developing high load web-application and trying to reduce the quantity of sql queries. Quite often I need to update one row and get the results. I think it would be nice to have possibility to run query and at the same time receive the values of updated fields without making 2 calls of mysql server. For example, I execute the following query:
update table set val=val+1 where id=1;
and function returns:
array("val"=>10)
Sure, I understand, that I can write my own function, which first makes update, than select and returns the result. But the problem is that in such case mysql server will have to seek data, update during first query, than comes the second query, which again requires to seek data and return it. And I am thinking about the way, where mysql seeks data, updates and returns updated data.

Related

When executing a query in rails, how do you view/log the actual query being executed?

I'm having trouble with a single query to one of my databases, and I think it's because the data I'm passing isn't being inserted correctly. The easiest way I can think of to try and check this would be to see the actual sql generated by my fairly simple query, which I call like so:
query = ActiveRecord::Base.connection.raw_connection.prepare(query_string)
result = query.execute(start_time, end_time)
This query, unfortunately, does not seem to log anything like queries made through activerecord - I have logged queries from before and after it, but the one made with execute says nothing at all. I know it's running the query because it does return results, just the wrong ones.
Is it failing to log the actual query because it's an execute statement? What do I need to do in order to see the actual sql query being sent out to the table here?

Possible places where table names will be used in a mysql query

I am trying to log the tables that a particular query might affect inside a DB wrapper function where all the DB calls will pass through.
I can't create a view or a transaction and execute the query and infer it from the resultset since it will increase the application performance time which I can't afford.
Instead I am trying to infer it from the query itself like table names will be the next word after 'FROM,JOIN,INSERT INTO' clauses. Is there any other places table names might be used in a MYSQL query?

How to create a new column with an SQL function in Pentaho PDI work-flow?

I have one sql function, let's say theFunction(item_id). It takes an item id and computes one value as its return. I read one table from the DB and I suppose to compute a new value to append for each row by this function given the item_id particular to taht row. Which desing block would do this form me with the following SQL (if not wrong).
select thsFunction(item_id);
I assume that the block gives me item_id of each row as a variable.
You can use another table input step, and have it accept fields from previous steps and execute for every row (both config options are at the bottom of the step's window).
Beware that this is a rather slow implementation. Each query is executed separately and as such each row requires a round trip to the database.
Alternatively, you can use the Row SQL Script. I believe it allows you to pass all SQL statements in a single trip to the database.
An SQL function is probably much more efficient to run in the database, for all rows at once, in stead of making a separate call into the database from PDI for each row to execute the function. So if performance is at all a relevant concern, I'd suggest a whole different strategy:
Write your rows to a table in the database. End your transformation here.
On the job level, first execute your transformation from above, then execute the function in an "Execute SQL script..." component, giving it an SQL command somewhat like "UPDATE my_temp_table set target_col = theFunction(item_id)".
Continue your job with the remaining steps in a new transformation, starting from that table as input.
This of course presupposes that you don't have too many other threads going on, but if your transofrmation is simple and linear -- or at least if it can be made single-linear at this particular step -- it may be possible to split it up into two parts before and after this SQL call.

Select SQL query FROM table, select using that query?

I'm trying to do the following in SQL alone. In the end it will end up on a wso2 DSS server but if it can be done in sql alone even better :)
Sudocode
Array results=Array;
result = <sql>select id, query from definitions</sql>
foreach result.query
r=<sql>query</sql>
results.push(r)
I am trying to run a select on table a that returns 2 columns.
One of the two columns is named query, and I want to then execute that query returning
id, query_title, query_text
We can assume that the query column always returns the same columns (through aliases written in the query)
The other option would be doing this in WSO2 DSS however I though at least that it can only do what sql does. Maybe joining with the ESB I could get it if that doesn't work but really my goal would be to do it ALL in sql as I am going to insert insert information and then update it into another table anyway.
You cannot do this with a single select query.
One solution is to do this in two steps. Fetch the query in the application using your SQL and then execute the second query from the application.
The second solution is to use a stored procedure and prepare/execute. How you then fetch the results depends on the nature of the results

Sending multiple mysql instructions through a jdbc connection in Google Script

I'd like to send several queries to the Google Cloud SQL database from Google Apps Script in one row. For example :
insert into table_name (field_name) values ("prout");
select last_insert_id()
but for some reason I can't get it to work. Is the API limited to one query per time ? It is a pain because sending a query takes time. It would be a lot more efficient to be able to send several things at once.
The reason that you may not be able to send multiple queries at the same time is because each statement will have a different return value. For example, an "insert" statement will give you an integer indicating how many rows were affected (hopefully 1!). A "select" statement will return to you a set of objects.
You can do batch commands using addBatch, but they must all be of the same type of query (for example, a lot of "insert"s).