Find('first'), Find('count'), or Field('id')...Which is faster? - mysql

What is better in terms of speed…
I am trying to determine whether or not a user has added a certain URL to their list of shortcuts. If they have added the URL there will be a link on the page to remove the page from the shortcuts otherwise they can add it to their shortcuts for quick access via a dropdown menu. Unfortunately I need to make this check at every page load so the code is in my AppController. I would like to do whatever I can to speed this up. I don't want this cached.
Would it be faster to do a find('first') while limiting the "fields" to just “id”, a find('count'), or a field('id') where the conditions of either statement would be 'URL' => $this->here. Only 1 or 0 results should be returned.

Assuming your table is indexed correctly you will likely not see a difference. Per #mark's comment, use whichever one suites your needs.
The logic of which one to use should be your main concern.
If you're only trying to see IF there is one, then using field makes the most sense, since it's limit 1 and only returns a single field.
If you want to know how many there are, then you'll need count.
And if you want to know IF there is one, and retrieve it's data, then first or exists is the way to go.

Related

Editing table row data populated with with classic asp

I'm hoping this will be a rather simple question to answer, as I'm not looking for any specific code. I have a table on a classic asp page populated from an sql server. I've just set the table up so that each row is clickable and takes you to a page to edit the data in the row. My question is this: Would I be better off trying to use the recordset that populated the table or should I reconnect to the db and pull just the record I want edited.
As always; It Depends. It depends on what you need to edit about the record. It depends on How far apart your DB and site are from each other. It depends on which machine, if the DB and site are on separate machines, is more powerful.
That being said, you should make a new call for that specific record. The reason mainly being because of a specification you made in your question:
...and takes you to a page to edit the data in the row
You should not try to pass a record set between pages. There are a few reasons for this
Only collect what you need
Make sure data is fresh
Consider how your program will scale
On point 1 there are two ways to look at this. One is that you are trying to pass the entire record set across a page when you only need 1 record. There are few situations where another DB call would cost more than this. The other is you are only passing one record which would make me question your design. Why does this record set have every item related to a record. You are selecting way too much for just a result list. Or if the record is that small then Why do you need the new page. Why can you not just reveal an edit template for the item if it is that minimal.
On point 2 consider the following scenario. You are discussing with a coworker how you need to change a customer's record. You pull up this result set in an application but then nature calls and you step away from you desk. The coworker gets called by the customer and asked why the record is not updated yet. To placate the customer your coworker makes the changes. Now you are using an old record set and may overwrite additional changes your coworker made while you were away. This all happens because you never update the record set, you always just pass the old one from page to page.
On point 3 we can look back a point 1 a bit. let us say that you are passing 5 fields now. You decide though that you need a comments field to attach to one of your existing fields. do you intend to pass 2000 characters of that comment field to the next page? How about if each of the 5 need a comment field? Do you intend to pass 10,000 characters for a properly paged record set of 10? do you not do record set paging and need to pass 10,000 characters for a full 126 records.
There are more reasons too. Will you be able to keep your records secure passing them this way? Will this effect your user's experience because they have a crummy computer and cannot build that quick of a post request quickly? Generally it is better to only keep what you need and in most situations your result set should not have everything you need to edit.

Why the order of rows changes in mysql upon update

I am displaying the contents of a table on a web page, I execute simply
SELECT * FROM TABLE_NAME
inside my java code
I am also using Ajax to allow user to change the contents of the rows, then without refreshing (thats what Ajax do) display the changes made.
Now the problem is when the changes are reflected the order changes, and it becomes difficult for the user.
Here's a image to explain it graphically
So I wanted to ask that
why this happens
How to prevent it
Note : I am not showing the primary key on the page, for obvious reasons
If you don't specify explicit ORDER BY - mysql (and any other RDBMS) does not guarantee any particular order.
An important note: even having ORDER BY doesn't guarantee stable result set though, until you have a unique column (or tuple) that participate in sorting.
So the answer is: add ORDER BY

Access Input textbox on Data Access page with only select enabled to allow filter by searchs

I have a data access page which gets all it's data from a query of another table, problem is all the textboxs I make on the page and bind the Control source to the matching column then don't allow me to input any text in to use the filter option to search all the results.
I don't want to be able to record any information but if I change page properties to from DataEntry = false too true then it no longer displays the whole section of textboxs.
Anybody know of a way round this or suggestions of other simple solutions to display certain data from the table or all the data from the query in the same/similar way as this?
Edit- Original issue which now requires me to find new solution.
I've used DAP quite a bit -- I even built DAP solutions at Microsoft, and they were wildly popular. I always preferred to use the SQL Server flavor of DAP.. and that allows you to use TWO different types of filters, the standard filter property, in addition to the serverFilter property. The serverFilter property is extremely powerful, and it's incredibly nice to be able to filter things in TWO different ways.
Hope that helps
-Aaron MCITP DBA
The original issue has now been resolved but I had also found these 1, 2 links which guide one through a search feature on DAP's.

Save and get arbitrary sort order in SQL Server

My client wants to sort products by drag & drop. The drag & drop part is easy with javascript.
My problem is how do I save and get the sort order?
I'm using .net c# and SQL Server 2008.
When I move a product and drop it in a new position I get the id of the product that's moved, product in front and product behind. With this data I want to update the sort order of products.
I was thinking of adding a field with position, but then I guess I have to update every item when position changes.
In general adding an additional position field is the only thing you can do, to get truly arbitrary ordering.
But you can implement it in several ways. Here are two ways I've implemented myself some time ago.
1. Method: Update all position values, by looping over your items and performing an UPDATE statement for every position.
This is easy to implement, but because of the many updates, it's not good for many items and/or large tables. Especially if you do it via Ajax and perform a complete re-ordering on every change in the list.
2. Method: Do a smart update of only the affected rows.
SELECT all items in the current sort order (The "old list") (Usually fast compared to an UPDATE statement)
Iterate over all items from the "new list" and compare each item to the item from the old list at the same position/index. If the items are the same, don't do anything
If the items are different find that item from the old list, which should actually be at that position and update its position value accordingly (Some lookup data structure might be useful here)
That way you only have to perform minimal database updates, but you'll have more complex code.
Personally I'd go with the first way, until the database updates actually become a performance problem.
We have a sort column but yes we have to re-index all rows as things change. You could mitigate this by assigning sort's in large enough increments to allow some level of movability before you have to do this, such as in 10's or 100's but that's not the best solution and I'd be interested to see what other ideas people have.
If you can capture each move programatically (with up and down buttons for example) then you can just swap the position numbers of the row moving and the row being moved. Make sure that you add new rows at the max position + 1.

Displaying search results dynamically as use interacts with controls

I have a website and want to display search results dynamically meaning that as the user interacts with controls and selects options, the search results are populated in realtime - i.e. the user doesnt need to click the search button.
The data is stored in a MySQL relational data base.
Now I know this is likely to lead to a large server load for a user-set above a certain size - are there anyways to mitigate this?
Max.
One way to mitigate the server load would be to introduce a slight timer delay before posting back to the server after each control is populated. If you give the user 3 seconds or so to input an additional field, the user may have time to add a search parameter. That could eliminate an extraneous query or two.
Also I always like to set a max numbers of results returned.