optimizing apache and mysql for android clients requests - android

I would like to know which apache/httpd parameters a relevant to set, if I expect a high number of connections from different IPs, getting same ammount of data in the same time. As example: I would have 10000 mobile devices that try to get data from server in the same time. The dataset is about 100k big and is generated by a php script. Each call make a query to the database. How apache(and mysql) should be configured to stay alive during this requests?
<?php
$con = mysql_connect( 'localhost', 'root', 'pass') or die('Could not connect' );
mysql_select_db('mydb', $con) or die('Could not select database.');
$result = mysql_query("SELECT * FROM results");
mysql_close($con);
$items = array();
while($row = mysql_fetch_array($result)){
$items[] = array(
's' => $row['sport'],
....
);
echo json_encode($items);
}
Regards!
Dimitri.

First of all use mysqli API and not mysql (http://it2.php.net/mysqli).
Then you MUST use the bind param technique (http://it2.php.net/manual/en/mysqli-stmt.bind-param.php) beacuse the generated query have so much better performance because mysql deosn't have to rebuild the eexecution plan of the query but only reexecute it.
Server side pay attention to doesn't slow MyISAM driver but InnoDB. If you can manage the table structure adopt well known normalization practice : http://en.wikipedia.org/wiki/Database_normalization.
Also do the code inside your while in async way so u can "parallelize" the execution of the code.

Related

Fetching Object value in mongodb through query

I am making a chat app through socket.io in Android through Node.js . I want to store history of message and username which I have already stored successfully in mongodb but I want to fetch message:Object values here.. can someone please help me in writing query.
This query results in the output which you can see in the image.
messagesCollection.find().toArray().then(function (docs){
console.log(JSON.stringify(docs))
I want to retrieve "message" & "senderNickname" value through query. Replies are highly appreciated.
Pass projection operators in a query,
messagesCollection.find({},{_id: 0, "message.message":1,"message.senderNickname":1})
.toArray().then(function (docs){console.log(JSON.stringify(docs))});
make _id: 1 if you want object id as well for each record.

How apply sql query to Firebase in Xamarin Android?

I need get return data from Firebase. In Sql query it would be like this:
Select * from * Group By field1, field2 HAVING count(*)=1
How to apply above to FireBase in Xamarin Android. So I have node "chats", here I want retrieve record only not repeated on field "user_email":
chats
|-...
|---date_and_time:
|---user_email:
|-...
|---date_and_time:
|---user_email:
...
at this moment I get whole data from "chat", here code:
public List<MessageContent> listOfContacts = new List<MessageContent>();
FirebaseClient firebase;
...
firebase = new FirebaseClient(GetString(Resource.String.database_name));
...
var firebaseContacts = await firebase.Child("chats")
.OnceAsync<MessageContent>();
foreach (var item in firebaseContacts)
listOfContacts.Add(item.Object);
Firebase's realtime database is a NoSQL database. Trying to directly map a SQL query to it is going to give you a rough time. I highly recommend reading NoSQL data modeling and watching Firebase for SQL developers.
In NoSQL databases you typically get more complex write operations and in return get more performant/scalable read operations. So in your case, that could mean that you actually store a list of the records that you're trying to query and update that list with every write.
Some previous questions that deal with similar scenarios can be found in this list.

MySQL to Sqlite sync from PHP to mobile app (iOS/Android)

To sync a mysql database with sqlite database using a PHP web app, I am going to
create a "view" mirror of SQLITE tables in MySQL
select changed data on mirror "view" in MySQL
use mysqldump to dump the sql for sqlite and save it in a file on server
download that file from mobile
import data directly to sqlite
another approach would be not using a middle "view" table and dump data using "SELECT" or mysqldump and parse it on the device or server, so it would be readable by sqlite
is there any disadvantages in creating a mirror "view" for each table that SQLITE needs on client devices? or is there another 'standard 'way to sync these 2 which i did not find on the internet?
Edit: after searching and coding some more finally i decided to go like this:
build "insert into ..." statement for sqlite from PHP on the server and save it to a zipped file
download the file on mobile and insert extracted sql directly to SQLITE
here is the php code for generating mysqldump like output:
/** key [SQLITE column name] => value [MySQL select column] */
$table_structure = array(
'id' => 'id',
'title' => 'title',
'type' => 'COALESCE(type, "")', /** u need to wrap any column that is nullable like this **/
'flags' => 'COALESCE(type, "")', /** u need to wrap any column that is nullable like this **/
'view_field' => 'view_field',
'view_year' => 'view_year',
....
....
'updated_at' => 'UNIX_TIMESTAMP(updated_at)');
$into_query = '';
$into_values = '';
$indexer = 0;
foreach ($table_structure as $sqlite => $mysql) {
$into_query .= (($indexer !== 0)? ',':'').$sqlite;
$into_values .= (($indexer !== 0)? ',",",':'').'"\'",'.$mysql.',"\'"';
$indexer++;
}
$output_filepath = "some where nice";
$query = 'SELECT CONCAT("INSERT INTO '.$table.'('.$into_query.') VALUES(",'.$into_values.',");") FROM '.$table.' ' ... "INTO OUTFILE $output_filepath CHARACTER SET utf8";
I hope it's a good approach and may help someone some day :)
Approach 1: using sqlite db file
whatever step you have mentioned in question is correct. this approach used when you have to send lots of data(many tables) from device to web/php vice versa.
its takes time in slower internet connection as well in 2G. in 3G its fast(its also depends on whatever code you have written at mobile/PHP to upload & download file).
make sure database file size is small if it large then make zip file which include database file.
Approach 2: make using json (SELECT *) parse in device
this one is very long process when you have lost of table which have lots of fields. you have to get data from table one by one, send it to server/php its send back data after process. then you have to again insert data in device table.
this is also depends on INTERNET as well you code. its too slow for devices and take more time.
common things in both approach send/receive data only when its needed or on those which require for devices/PHP. do not send unused data to work fast.

same result in number of rows in mysql with php

I work at android application that connect with database in server
I need to check log in by user name and password
If the user name and password found in database the user found
I add in table log in usernames and passwords but the result of code is no
$name = $_POST["user_name"];
$password = $_POST["password"];
$result = mysql_query("SELECT user_name,password from login WHERE user_name = '$name' AND password = '$password'");
$no_of_rows = mysql_num_rows($result);
if ($no_of_rows == 1) {
$json = array("result"=>"yes");
} else {
$json = array("result"=>"no");
}
header('Content-Type: application/json');
echo json_encode($json)
I add in table login usernames and passwords but the result of code is no if username and password founded or not
what is the problem in this code ??
This can be due to several reasons:
The mysql_query is returning an error. I'd recommend to check the returning result with:
if ($result === FALSE)
echo "I have an error in my MySQL sentence\n";
It's not mandatory, but I don't know whether you have more than one opened MySQL socket. That's why it's always a good idea to specify the link id to the mysql_query function, e.g.:
$mylink = mysql_connect($host, $login, $password);
$result = mysql_query("SELECT ...", $mylink);
It's always a good idea to test the sentences you're testing in your code manually. So I'd put echo "SELECT user_name,password from login WHERE user_name = '$name' AND password = '$password'\n";, then copy it, manually connect to the MySQL server and execute it and see whether it's returning anything (in case it returns something, that means that the error is in the code).
It's not a debugging tip, but I'd recommend always encrypting the passwords in the database. So when you insert a login in the database, instead of inserting ('$user', '$pass'), better insert ('$user', MD5('$pass')), for a better security. For the SELECTs it's the same: SELECT * FROM table WHERE login = '$user' AND pass = MD5('$pass').
Hope this helps!

what is the exact process of using the Database in phonegap android?

I am new to Phonegap. I am trying to implement an application in Phonegap Android. For the past two days,I am scouting for a proper way of accessing the database and retrieving it from the same.I did not find an answer to my questions
I have learnt that Phonegap does not support SQLLITE but supports the W3C Web SQL Database Specification and W3C Web Storage.
At the same time I noticed few plugins for Phonegap 1.5..which does not exist now. At the same time i found that W3c database provides a limited storage of 5MB for iOS.
I found this SQL Plugin for Phonegap Android PhoneGap-SQLitePlugin-Android Is it advicable to use this or any other method. Please,guide me.
So,if you have any sort of example of accessing the database that can be followed please share it.
I made an app recently that required this, targetting the Android and iOS. You can use a combination of the following ::
1. LocalStorage ::
Check for localStorage
function supports_html5_storage() {
try {
return 'localStorage' in window && window['localStorage'] !== null;
} catch (e) {
return false;
}
}
Set an item into LocalStorage
localStorage.setItem("bar", foo);
or
localStorage["bar"] = foo;
Get an item from LocalStorage
var foo = localStorage.getItem("bar");
or
var foo = localStorage["bar"];
2. SQLite Database (more convenient, more persistive)
Set up your DB
var shortName = 'BHCAppDB';
var version = '1.0';
var displayName = 'BHCAppDB';
var maxSize = 65535;
if (!window.openDatabase){
alert('!! Databases are not supported in this Device !! \n\n We are sorry for the inconvenience and are currently working on a version that will work on your phone');
}
db = openDatabase(shortName, version, displayName,maxSize);
createAllTables(db);
Create your Tables
function createAllTables(db){
db.transaction(function(transaction){
transaction.executeSql("CREATE TABLE IF NOT EXISTS Profile(id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT, gender TEXT,age INTEGER)");
}
Execute an SQL Query
transaction(function(transaction){
var rowCount = 'SELECT * FROM Profile';
transaction.executeSql(rowCount,[],function(transaction,result){
if(result.rows.length == 0){
var sqlString = 'INSERT INTO Profile (name,gender,age) VALUES("自己","Female",18)';
transaction.executeSql(sqlString);
}
});
});
3. Native Storage on all devices
This is the best part of Phonegap. You can call a native plugin class on all the devices using the Phonegap plugin call. During the call, you can pass parameters to the class, and the native class can store your data in the OS itself.
For example :: in iOS, you create a plugin .h & .m class and register it with the Cordova.plist file. Once that's done, you need to send a call to the class from JavaScript using Phonegap. Once the parameters have been received using NSDictionary or any other NSArray type, you can call a CoreData class to store UNLIMITED amounts of data. You'll never run out of memory .
This can be done in a similar fashion for all the rest of the OS's also :)
For Encryption try the following :: SQLCipher
Here is some additional information on working with an existing SQLite database. In this example encrypted.db is that brand new database you create and pragma.
ATTACH DATABASE 'encrypted.db' AS encrypted KEY 'secret'; -- create a new encrypted database
CREATE TABLE encrypted.t1(a,b); -- recreate the schema in the new database (you can inspect all objects using SELECT * FROM sqlite_master)
INSERT INTO encrypted.t1 SELECT * FROM t1; -- copy data from the existing tables to the new tables in the encrypted database
DETACH DATABASE encrypted;

Categories

Resources